# Automatic reindexing of associated columns acts\_as\_taggable

**URL:** <https://www.ruby-forum.com/t/automatic-reindexing-of-associated-columns-acts-as-taggable/69024>\
**Category:** Ferret\
**Created:** [September 18, 2006, 6:32am UTC](https://www.ruby-forum.com/t/automatic-reindexing-of-associated-columns-acts-as-taggable/69024 "2006-09-18T06:32:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sid137](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bb73d2/32.png) [@sid137](https://www.ruby-forum.com/u/sid137)\
**Post date:** [September 18, 2006, 6:32am UTC](https://www.ruby-forum.com/t/automatic-reindexing-of-associated-columns-acts-as-taggable/69024/1 "2006-09-18T06:32:27Z")

</div>

Hi,

So i’m trying to use acts\_as\_taggable with the acts\_as\_ferret plugin,  
where I have Post.rb model, which has a method tag\_list made available  
through acts\_as\_taggable, as returns a string of associated tag words  
from the tags table (tag.rb). I’ve set up my Post.rb model in the  
following way.

class Post \< ActiveRecord::Base  
acts\_as\_taggable  
acts\_as\_ferret :fields =\> [“title”, “description”, :tag\_list]

…

end

I’m noticing that when I submit a new post, the titles and descriptions  
are automatically indexed, and are searchable with ferret… However,  
the tags do not show up in the results when I do a tag search. However,  
once I use script console and try to manually rebuild the Post index:

Post.rebuild\_index(Post)

(is this syntax even correct? it returns a result of false)

the tags immediately start to appear in the tag search results. So is  
there a way to set ferret to automatically rebuild the tag index when a  
Post is saved? I imagine it probably shouldn’t rebuild the entire index  
for all posts every time a post is saved, as that might slow things up  
eventually, right? So what would be a good solution…?

Thanks,  
-Sidney

---

<div class="post-metadata">

**Author:** ![sid137](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bb73d2/32.png) [@sid137](https://www.ruby-forum.com/u/sid137)\
**Post date:** [September 18, 2006, 2:09pm UTC](https://www.ruby-forum.com/t/automatic-reindexing-of-associated-columns-acts-as-taggable/69024/2 "2006-09-18T14:09:40Z")

</div>

Hi!

On Mon, Sep 18, 2006 at 06:32:27AM +0200, Sidney B. wrote:

> acts\_as\_ferret :fields =\> [“title”, “description”, :tag\_list]  
> Post.rebuild\_index(Post)
> 
> (is this syntax even correct? it returns a result of false)

you can omit the parameter in this case, as Post is the class you call  
rebuild\_index upon. But doesn’t hurt either.

> the tags immediately start to appear in the tag search results. So is  
> there a way to set ferret to automatically rebuild the tag index when a  
> Post is saved? I imagine it probably shouldn’t rebuild the entire index  
> for all posts every time a post is saved, as that might slow things up  
> eventually, right? So what would be a good solution…?

I don’t remember right now, how acts\_as\_taggable works, but it  
seems the tags are set after the post is indexed. Calling  
post.ferret\_update after applying the tags to the post object should  
reindex the object with tags included (this should work in 0.2.x  
versions of aaf, too). If you are concerned about performance, and use  
aaf 0.3,  
you can also suppress the first indexing of the non-tagged post:

post = Post.new(…)  
post.disable\_ferret  
post.save

# set tags here …

post.ferret\_update

or even more elegant:

post.disable\_ferret(:index\_when\_finished) do  
post.save

# set tags here

end

which calls ferret\_update after executing the block.

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:** ![sid137](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bb73d2/32.png) [@sid137](https://www.ruby-forum.com/u/sid137)\
**Post date:** [September 19, 2006, 6:56am UTC](https://www.ruby-forum.com/t/automatic-reindexing-of-associated-columns-acts-as-taggable/69024/3 "2006-09-19T06:56:03Z")

</div>

Nice, thats awesome… thanks alot!

---

<div class="post-metadata">

**Author:** ![sid137](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bb73d2/32.png) [@sid137](https://www.ruby-forum.com/u/sid137)\
**Post date:** [September 18, 2006, 3:05pm UTC](https://www.ruby-forum.com/t/automatic-reindexing-of-associated-columns-acts-as-taggable/69024/4 "2006-09-18T15:05:46Z")

</div>

I was noticing a similar problem as well… This looks like the  
solution for me! thanks…

Eric
