Forum: Ruby on Rails ActiveRecord callbacks not happening

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bcfc926d36e15709d7e6c70b9791211a?d=identicon&s=25 Vamsee Kanakala (Guest)
on 2006-01-05 17:44
(Received via mailing list)
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.
5fc52b212d554dd8e0337bf27a5187f3?d=identicon&s=25 Lee Marlow (Guest)
on 2006-01-05 17:44
(Received via mailing list)
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
Bcfc926d36e15709d7e6c70b9791211a?d=identicon&s=25 Vamsee Kanakala (Guest)
on 2006-01-06 07:28
(Received via mailing list)
Lee Marlow wrote:

>I don't think after_destroy should be a class method.
>
Thanks Lee, changing it to an instance method did the trick.

Vamsee.
This topic is locked and can not be replied to.