# Automatically Indexing Associated Models

**URL:** <https://www.ruby-forum.com/t/automatically-indexing-associated-models/84068>\
**Category:** Ferret\
**Created:** [February 1, 2007, 2:42am UTC](https://www.ruby-forum.com/t/automatically-indexing-associated-models/84068 "2007-02-01T02:42:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [February 1, 2007, 2:42am UTC](https://www.ruby-forum.com/t/automatically-indexing-associated-models/84068/1 "2007-02-01T02:42:38Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [February 1, 2007, 9:33am UTC](https://www.ruby-forum.com/t/automatically-indexing-associated-models/84068/2 "2007-02-01T09:33:49Z")

</div>

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](http://www.webit.de)  
Dipl.-Wirtschaftsingenieur Jens Krämer removed\_email\_address@domain.invalid  
Schnorrstraße 76 Tel +49 351 46766 0  
D-01069 Dresden Fax +49 351 46766 66

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [February 1, 2007, 2:54pm UTC](https://www.ruby-forum.com/t/automatically-indexing-associated-models/84068/3 "2007-02-01T14:54:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [February 1, 2007, 3:34pm UTC](https://www.ruby-forum.com/t/automatically-indexing-associated-models/84068/4 "2007-02-01T15:34:28Z")

</div>

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 🙂

Jens

–  
webit! Gesellschaft für neue Medien mbH [www.webit.de](http://www.webit.de)  
Dipl.-Wirtschaftsingenieur Jens Krämer removed\_email\_address@domain.invalid  
Schnorrstraße 76 Tel +49 351 46766 0  
D-01069 Dresden Fax +49 351 46766 66

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [June 13, 2007, 1:23am UTC](https://www.ruby-forum.com/t/automatically-indexing-associated-models/84068/5 "2007-06-13T01:23:55Z")

</div>

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 🙂
> 
> Jens
> 
> –  
> webit! Gesellschaft fï¿½r neue Medien mbH [www.webit.de](http://www.webit.de)  
> Dipl.-Wirtschaftsingenieur Jens Krï¿½mer removed\_email\_address@domain.invalid  
> 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?
