x6iae
x6iae

Reputation: 4164

Automatically re-indexing Sunspot/Solr on edit

I am using Sunspot wich is a Solr-based search engine on my rails app.

According to this Rails Cast episode on Search with Sunspot:

Sunspot automatically indexes any new records but not existing ones. We can tell Sunspot to reindex the existing records by running: rake sunspot:reindex

Which should basically reindex the whole Model.

However, I have a Model (TicketSubject), with an attribute, category, and I will like to reindex a ticket_subject each time there is an edit on its category.

How can I go about this?

Upvotes: 1

Views: 531

Answers (1)

Jones Agyemang
Jones Agyemang

Reputation: 1516

Why don't you use an ActiveRecord callback for this i.e. after_update?

after_update() Is called after Base.save on existing objects that have a record. Note that this callback is still wrapped in the transaction around save. For example, if you invoke an external indexer at this point it won‘t see the changes in the database. http://api.rubyonrails.org/v2.3.8/classes/ActiveRecord/Callbacks.html#M001377

Define: Callbacks are hooks into the life cycle of an Active Record object that allow you to trigger logic before or after an alteration of the object state. http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

Upvotes: 1

Related Questions