# Best way to maintain dynamic fields?

**URL:** <https://www.ruby-forum.com/t/best-way-to-maintain-dynamic-fields/91210>\
**Category:** Ferret\
**Created:** [April 5, 2007, 5:44pm UTC](https://www.ruby-forum.com/t/best-way-to-maintain-dynamic-fields/91210 "2007-04-05T17:44:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgrosjean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecc23a/32.png) [@jgrosjean](https://www.ruby-forum.com/u/jgrosjean)\
**Post date:** [April 5, 2007, 5:44pm UTC](https://www.ruby-forum.com/t/best-way-to-maintain-dynamic-fields/91210/1 "2007-04-05T17:44:53Z")

</div>

I have a Topic that has\_many comments. In the search results only want  
Topics to show up, but I want them to include the text of the comments  
that they have when they are indexed. To do this I’ve added a  
:comments\_text field for acts\_as\_ferret and the comments\_text method  
gathers the text from all comments up into a string that can be indexed.

The trick is that when comments are added, updated, or deleted the Topic  
needs to be updated in the ferret index, and I’m not sure what the best  
way to enforce that is. I’ve included my current code to do this, which  
seems to work, but I’d like to know if there is a better way to maintain  
this dependency then what I’m doing. In particular am I using  
appropriate callback methods in Comment to trigger the updates on Topic  
(ie. after\_create, after\_update, after\_destroy). And to make the destroy  
case work I’m needing to call unless each.frozen? in the comments\_text  
method… is that the right thing to do?

Thanks. Here’s my existing code:

class Topic \< ActiveRecord::Base  
acts\_as\_ferret :store\_class\_name =\> true, :fields =\> {  
:comments\_text =\> { :store =\> :yes } }

has\_many :comments, :as =\> :commentable, :dependent =\> :destroy

def comments\_text  
result = “”  
for each in comments  
unless each.frozen?  
result \<\< " "  
result \<\< each.subject  
result \<\< " "  
result \<\< each.body  
end  
end  
result  
end  
end

class Comment \< ActiveRecord::Base  
belongs\_to :commentable, :polymorphic =\> true

def after\_create  
commentable.ferret\_update  
end

def after\_update  
commentable.ferret\_update  
end

def after\_destroy  
commentable.ferret\_update  
end  
end

---

<div class="post-metadata">

**Author:** ![jgrosjean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecc23a/32.png) [@jgrosjean](https://www.ruby-forum.com/u/jgrosjean)\
**Post date:** [April 5, 2007, 9:36pm UTC](https://www.ruby-forum.com/t/best-way-to-maintain-dynamic-fields/91210/2 "2007-04-05T21:36:40Z")

</div>

Jens,

Thanks for your help and for your work on this cool plugin. I didn’t  
know about the reload option, that works great.

I was just going to complain that multi\_search didn’t return the results  
in the proper score order (it was returning them sorted by Class, and  
then store), but I just check out trunk and it works great there. So  
thanks again.

Jesse

---

<div class="post-metadata">

**Author:** ![jgrosjean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecc23a/32.png) [@jgrosjean](https://www.ruby-forum.com/u/jgrosjean)\
**Post date:** [April 5, 2007, 8:16pm UTC](https://www.ruby-forum.com/t/best-way-to-maintain-dynamic-fields/91210/3 "2007-04-05T20:16:21Z")

</div>

On Thu, Apr 05, 2007 at 05:44:53PM +0200, Jesse G. wrote:

> this dependency then what I’m doing. In particular am I using  
> appropriate callback methods in Comment to trigger the updates on Topic  
> (ie. after\_create, after\_update, after\_destroy). And to make the destroy  
> case work I’m needing to call unless each.frozen? in the comments\_text  
> method… is that the right thing to do?

Well, calling frozen? is not an extraordinary clean solution (in terms  
of code readability - one does not easily understand from reading the  
code why it is done), but probably the most efficient one.

Doing a commentable.reload before calling ferret\_update should work  
without the check for frozen?, because the newly loaded collection  
will not include the deleted comment any more.  
So, in the spirit of ‘premature optimization is the root of all evil’  
you would probably do that instead and do the frozen? optimization  
only once the need arises (i.e. things get slow because of the  
additional DB usage).

Or just write a comment explaining the frozen? check…

Jens

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

Amtsgericht Dresden | HRB 15422  
GF Sven Haubold, Hagen Malessa
