Skip to content

Instantly share code, notes, and snippets.

@haifeng
Forked from nz/sunspot_resque.rb
Created October 13, 2011 16:45
Show Gist options
  • Save haifeng/1284752 to your computer and use it in GitHub Desktop.
Save haifeng/1284752 to your computer and use it in GitHub Desktop.
Sunspot with Resque
# app/models/post.rb
class Post
searchable :auto_index => false, :auto_remove => false do
text :title
text :body
end
after_commit :resque_solr_update
before_destroy :resque_solr_remove
protected
def resque_solr_update
Resque.enqueue(SolrUpdate, Post, id)
end
def resque_solr_remove
Resque.enqueue(SolrRemove, Post, id)
end
end
# lib/jobs/solr_update.rb
class SolrUpdate
@queue = :solr
def self.perform(klass, id)
klass.find(id).solr_index
end
end
# lib/jobs/solr_remove.rb
class SolrRemove
@queue = :solr
def self.perform(klass, id)
Sunspot.remove_by_id(klass, id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment