ActiveRecord callbacks not happening


#1

Hi,

I have a model class like so:

class Candidate < ActiveRecord::Base
validates_presence_of :name
@@index = FerretConfig::INDEX

def self.after_destroy
puts “Ferret: after_destroy called”
@@index.query_delete("+id:#{self.id}
+ferret_class:#{self.class.name}")
optimize_index
end
end

But when I delete a record, the after_destroy is never called. Nothing
shown on logs from the puts statement. Am I missing something?

Thanks,
Vamsee.


#2

I don’t think after_destroy should be a class method. Try changing it
to

def after_destroy

end

One thing to watch out for is the ferret_class will be tokenized when
using query_delete. So, ‘Candidate’ will become ‘candidate’. You
might want to try creating a Boolean query with a TermQuery in it for
the ferret class.

Here is the query I use:

bq = Search::BooleanQuery.new
class_term = Ferret::Index::Term.new(‘ferret_class’, self.class.name)
bq.add_query(Ferret::Search::TermQuery.new(class_term),
Search::BooleanClause::Occur::MUST)
id_term = Index::Term.new(‘id’, self.id.to_s)
bq.add_query(Search::TermQuery.new(id_term),
Search::BooleanClause::Occur::MUST)

I hope that helps.

-Lee


#3

Lee M. wrote:

I don’t think after_destroy should be a class method.

Thanks Lee, changing it to an instance method did the trick.

Vamsee.