general setup for searchify in rails, how to access variable from initializer file

289 views Asked by At

how do i access a variable from an initializer file?

i have a file called search.rb in my initializer folder

api_client = IndexTank::Client.new 'http://:[email protected]'
index = api_client.indexes 'idx'

however, in my controller whenever im trying to index a newly created lesson, rails gives me an error of

undefined method `document' for nil:NilClass

my controller is...

def create
    index.document(@lesson.id).add({:text => @lesson.content })
end

also is this a bad way of indexing my documents? whenever they're being created? thanks

2

There are 2 answers

0
Chris Lamprecht On BEST ANSWER

You might find it useful to look at the Tanker gem by kidpollo - https://github.com/kidpollo/tanker

It's a 3rd party IndexTank gem for rails. You don't actually have to use it - you can keep using the indextank gem if you want. But looking at the source of the Tanker gem may be helpful in figuring out the best way to write your code under rails.

1
Iaroslav Vorozhko On

Looks like index object was not initialized.

NO, it isn't a bad way for indexing your documents. But, I suggest to move indexing in special Index model which will handle all indexing logic for the particular index. Think of index like of database table.