Automatically Indexing Associated Models

PROBLEM
I have two models, Blog and BlogComment. When a blog is initially
created, it has no comments. Upon creation, the title and body are
automatically added to the ferret index and directly searchable.
However, when a comment is added to a blog, that comment does not get
added to the index and is therefore not ferretable. The desired behavior
is that when a comment is added to a blog, that the comment be
ferretable.

CURRENT SETUP
Blog (id, title, body, user_id)
BlogComment (id, blog_id, comment)

class Blog < ActiveRecord::Base
has_many :blog_comments, :dependent => :destroy
acts_as_ferret :additional_fields => [:blog_comments]

def blog_comments
self.blog_comments.collect {|comment| comment.body }
end

end

class BlogComment < ActiveRecord::Base
belongs_to :blog
end

CONTROLLER
[…]
if @blog.blog_comments << comment
do_something
else
do_something
end

Can someone recommend a good approach to automatically updating the
index when a comment is added?

On Thu, Feb 01, 2007 at 02:42:38AM +0100, Mark wrote:

Blog (id, title, body, user_id)
end

class BlogComment < ActiveRecord::Base
belongs_to :blog
end

CONTROLLER
[…]
if @blog.blog_comments << comment
do_something

adding
@blog.ferret_update
here should do the trick.

else
do_something
end

Jens


webit! Gesellschaft für neue Medien mbH www.webit.de
Dipl.-Wirtschaftsingenieur Jens Krämer [email protected]
Schnorrstraße 76 Tel +49 351 46766 0
D-01069 Dresden Fax +49 351 46766 66

Jens K. wrote:

if @blog.blog_comments << comment
do_something

adding
@blog.ferret_update
here should do the trick.

else
do_something
end

Jens, that’s one option I had considered. What do you think about
creating an onsave event in all the models that use acts_as_ferret like
so…

after_save :update_ferret_index

def update_ferret_index
self.blog.ferret_update if self.blog
end

On Thu, Feb 01, 2007 at 02:54:41PM +0100, Mark wrote:

do_something
end
yeah, this should work, too. Even prettier from an architectural point
of view :slight_smile:

Jens


webit! Gesellschaft für neue Medien mbH www.webit.de
Dipl.-Wirtschaftsingenieur Jens Krämer [email protected]
Schnorrstraße 76 Tel +49 351 46766 0
D-01069 Dresden Fax +49 351 46766 66

Jens K. wrote:

On Thu, Feb 01, 2007 at 02:54:41PM +0100, Mark wrote:

do_something
end
yeah, this should work, too. Even prettier from an architectural point
of view :slight_smile:

Jens


webit! Gesellschaft f�r neue Medien mbH www.webit.de
Dipl.-Wirtschaftsingenieur Jens Kr�mer [email protected]
Schnorrstra�e 76 Tel +49 351 46766 0
D-01069 Dresden Fax +49 351 46766 66

Hi. i have the same problem, but my_instance.ferret_update doesn’t index
associated model fields on the console. Doesn’t ferret_update get called
after_save anyway?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs