# Acts\_as\_ferret and associations

**URL:** <https://www.ruby-forum.com/t/acts-as-ferret-and-associations/74424>\
**Category:** Ferret\
**Created:** [November 6, 2006, 7:53pm UTC](https://www.ruby-forum.com/t/acts-as-ferret-and-associations/74424 "2006-11-06T19:53:49Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [November 6, 2006, 7:53pm UTC](https://www.ruby-forum.com/t/acts-as-ferret-and-associations/74424/1 "2006-11-06T19:53:49Z")

</div>

I have the following models:

```
class Book < ActiveRecord::Base
  acts_as_ferret
  belongs_to :author
end

class Author < ActiveRecord::Base
    has_many :books
end

```

and in the controller:

def search  
if params[:query]  
@query = params[:query]  
@total, @books = Book.full\_text\_search(@query, :page =\>  
(params[:page]||1))  
@pages = pages\_for(@total)  
else  
@books = []  
end  
end

I can use the acts\_as\_ferret plugin to search for a book by title but it  
isn’t picking up the authors through the association. So I can’t do a  
search for books by author.

I’ve a feeling I’ve left some config out here. Can anyone help me out?

---

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [November 6, 2006, 11:47pm UTC](https://www.ruby-forum.com/t/acts-as-ferret-and-associations/74424/2 "2006-11-06T23:47:03Z")

</div>

Hi Matthew

I’m fairly new to AAF as well, but I think if you read through this:  
[http://projects.jkraemer.net/acts\_as\_ferret/rdoc/classes/FerretMixin/Acts/ARFerret/ClassMethods.html#M000006](http://projects.jkraemer.net/acts_as_ferret/rdoc/classes/FerretMixin/Acts/ARFerret/ClassMethods.html#M000006)  
it might help you. I think essentially it boils down to the fact that  
attributes are indexed by default but associations are not, so you might  
have to pass some options in your call to acts\_as\_ferret.

Pete.

---

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [November 7, 2006, 12:08am UTC](https://www.ruby-forum.com/t/acts-as-ferret-and-associations/74424/3 "2006-11-07T00:08:10Z")

</div>

To clarify, say your Author class has the fields, ‘first\_name’ and  
‘surname’. To have these indexed by the Book class you would need to do  
some things:

1. Create methods in Book.rb to access these fields:

def author\_first\_name  
return author.first\_name  
end

def author\_surname  
return author\_surname  
end

1. Pass these fields as options to the call to acts\_as\_ferret:

class Book \< ActiveRecord::Base  
acts\_as\_ferret :additional\_fields =\> [‘author\_first\_name’,  
‘author\_surname’]  
belongs\_to :author  
end

I’m not sure it there’s a more elegant solution, but that should do the  
trick.

Pete.

---

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [November 7, 2006, 10:22am UTC](https://www.ruby-forum.com/t/acts-as-ferret-and-associations/74424/4 "2006-11-07T10:22:20Z")

</div>

Thanks Pete. I’ll give this a go.

Do you mean:

> def author\_surname  
> return author.surname  
> end

and not:

> def author\_surname  
> return author\_surname  
> end

M.

---

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [November 7, 2006, 11:15am UTC](https://www.ruby-forum.com/t/acts-as-ferret-and-associations/74424/5 "2006-11-07T11:15:41Z")

</div>

Jens K. wrote:

> that’s exactly what I would have suggested, but please use symbols for  
> field names in your call to acts\_as\_ferret.

So:

class Book \< ActiveRecord::Base  
acts\_as\_ferret :additional\_fields =\> [:author\_first\_name,  
:author\_surname]  
belongs\_to :author

def author\_first\_name  
return author.first\_name  
end

def author\_surname  
return author.surname  
end

end

?

---

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [November 7, 2006, 11:22am UTC](https://www.ruby-forum.com/t/acts-as-ferret-and-associations/74424/6 "2006-11-07T11:22:40Z")

</div>

Matthew Planchant wrote:

> class Book \< ActiveRecord::Base  
> acts\_as\_ferret :additional\_fields =\> [:author\_first\_name,  
> :author\_surname]  
> belongs\_to :author
> 
> def author\_first\_name  
> return author.first\_name  
> end
> 
> def author\_surname  
> return author.surname  
> end  
> end

This works. Thanks.

---

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [November 7, 2006, 11:35am UTC](https://www.ruby-forum.com/t/acts-as-ferret-and-associations/74424/7 "2006-11-07T11:35:38Z")

</div>

Will this work with many-to-many relationships?

For example:

class Book \< ActiveRecord::Base  
acts\_as\_ferret :additional\_fields =\> [:topic\_title]

has\_many :book\_topics, :dependent =\> true  
has\_many :topics, :through =\> :book\_topics

def topic\_title  
return topic.title  
end

end

---

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [November 7, 2006, 11:09am UTC](https://www.ruby-forum.com/t/acts-as-ferret-and-associations/74424/8 "2006-11-07T11:09:07Z")

</div>

On Tue, Nov 07, 2006 at 12:08:10AM +0100, Pete Royle wrote:  
[…]

> 1. Create methods in Book.rb to access these fields:  
> […]
> 2. Pass these fields as options to the call to acts\_as\_ferret:
> 
> class Book \< ActiveRecord::Base  
> acts\_as\_ferret :additional\_fields =\> [‘author\_first\_name’,  
> ‘author\_surname’]  
> belongs\_to :author  
> end
> 
> I’m not sure it there’s a more elegant solution, but that should do the  
> trick.

that’s exactly what I would have suggested, but please use symbols for  
field names in your call to acts\_as\_ferret.

cheers,  
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:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [November 7, 2006, 3:03pm UTC](https://www.ruby-forum.com/t/acts-as-ferret-and-associations/74424/9 "2006-11-07T15:03:58Z")

</div>

On Tue, Nov 07, 2006 at 11:35:38AM +0100, Matthew Planchant wrote:

> def topic\_title  
> return topic.title  
> end

this won’t work, as there is no method ‘topic’ in your Book class.  
But you could index the titles of all topics:

acts\_as\_ferret :additional\_fields =\> [:topic\_titles]  
def topic\_titles  
topics.collect { |topic| topic.title }.join ’ ’  
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:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [November 7, 2006, 3:13pm UTC](https://www.ruby-forum.com/t/acts-as-ferret-and-associations/74424/10 "2006-11-07T15:13:14Z")

</div>

Jens K. wrote:

> On Tue, Nov 07, 2006 at 11:35:38AM +0100, Matthew Planchant wrote:
> 
> > def topic\_title  
> > return topic.title  
> > end
> 
> this won’t work, as there is no method ‘topic’ in your Book class.  
> But you could index the titles of all topics:
> 
> acts\_as\_ferret :additional\_fields =\> [:topic\_titles]  
> def topic\_titles  
> topics.collect { |topic| topic.title }.join ’ ’  
> end

Thanks. I’ll give this a shot.

---

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [November 16, 2006, 7:04am UTC](https://www.ruby-forum.com/t/acts-as-ferret-and-associations/74424/11 "2006-11-16T07:04:55Z")

</div>

Hi there, I have a similar problem. I’m building a CMS and we have a  
tree model called component\_instances. This model has polymorphic  
associations with many other models (e.g. Links, Folders, Pages etc…)  
I want the user to be able to search for an instance using data stored  
in the associated models. To start with in the backend interface, I only  
want them to be able to search for items by name, so I came up with  
this:

class ComponentInstance \< ActiveRecord::Base  
has\_many :permissions  
has\_many :groups, :through =\> :permissions  
belongs\_to :component #will be in different database ??  
acts\_as\_tree :order =\> ‘position’  
acts\_as\_list :scope =\> ‘parent\_id’  
belongs\_to :instance, :polymorphic =\> true  
acts\_as\_ferret(  
:fields =\> :instance\_name  
)

def instance\_name  
instance.name  
end

end

Now that looks like it should work. But when I search for an item by  
name (knowing that it exists), nothing shows. I know the search itself  
works because becure I added the :fields condition, it would pick up and  
return results for the word “folder” as it was used to define the  
polymorphic associations on the instances table.

So firstly your help with that problem would be most appreciated. Then  
we make things trickier by adding the fact that I’d like the end users  
on the front end to be able to search the site not just using the name  
field, but basically anything in any of the instance tables.

As a laughing point, when I wrote the original application in PHP using  
Mysql fulltext search, the query for the frontend search was 2 pages  
long! 🙂

Looking forward to your great ideas! 🙂

---

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [November 16, 2006, 6:30pm UTC](https://www.ruby-forum.com/t/acts-as-ferret-and-associations/74424/12 "2006-11-16T18:30:49Z")

</div>

Hi!

On Thu, Nov 16, 2006 at 07:04:55AM +0100, Brendon M. wrote:  
[…]

> Now that looks like it should work. But when I search for an item by  
> name (knowing that it exists), nothing shows. I know the search itself  
> works because becure I added the :fields condition, it would pick up and  
> return results for the word “folder” as it was used to define the  
> polymorphic associations on the instances table.

have a look in your development log when you save an instance of class  
ComponentInstance. There should be a line like  
‘adding field instance\_name with value …’  
showing what value aaf indexed for your instance name.

Maybe the instance isn’t there yet when the save takes place ?

> So firstly your help with that problem would be most appreciated. Then  
> we make things trickier by adding the fact that I’d like the end users  
> on the front end to be able to search the site not just using the name  
> field, but basically anything in any of the instance tables.

that’s easy, just index all the fields you want for frontend and backend  
search (including the instance\_name field) and use a special QueryParser  
restricted to only the instance\_name field for your backend search:

QueryParser qp = QueryParser.new(:fields =\> [:instance\_name])  
ComponentInstance.find\_by\_contents(qp.parse user\_query)

with this solution, backend users could still manually construct  
complex queries to search other fields, but queries not using any field  
names will default to only search the instance\_name field.

> As a laughing point, when I wrote the original application in PHP using  
> Mysql fulltext search, the query for the frontend search was 2 pages  
> long! 🙂

dont tell me any details 😉

cheers,  
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:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [November 17, 2006, 3:20am UTC](https://www.ruby-forum.com/t/acts-as-ferret-and-associations/74424/13 "2006-11-17T03:20:43Z")

</div>

Jens K. wrote:

> Hi!
> 
> On Thu, Nov 16, 2006 at 07:04:55AM +0100, Brendon M. wrote:  
> […]
> 
> > Now that looks like it should work. But when I search for an item by  
> > name (knowing that it exists), nothing shows. I know the search itself  
> > works because becure I added the :fields condition, it would pick up and  
> > return results for the word “folder” as it was used to define the  
> > polymorphic associations on the instances table.
> 
> have a look in your development log when you save an instance of class  
> ComponentInstance. There should be a line like  
> ‘adding field instance\_name with value …’  
> showing what value aaf indexed for your instance name.
> 
> Maybe the instance isn’t there yet when the save takes place ?

I already had a few records in the database so I assumed when i added  
the ferrit aa that it would index those. I will try adding a new record  
and see what it says when i get back to work on Monday. 🙂

> > So firstly your help with that problem would be most appreciated. Then  
> > we make things trickier by adding the fact that I’d like the end users  
> > on the front end to be able to search the site not just using the name  
> > field, but basically anything in any of the instance tables.
> 
> that’s easy, just index all the fields you want for frontend and backend  
> search (including the instance\_name field) and use a special QueryParser  
> restricted to only the instance\_name field for your backend search:
> 
> QueryParser qp = QueryParser.new(:fields =\> [:instance\_name])  
> ComponentInstance.find\_by\_contents(qp.parse user\_query)
> 
> with this solution, backend users could still manually construct  
> complex queries to search other fields, but queries not using any field  
> names will default to only search the instance\_name field.

Excellent! Sounds like a good solution. Will this mean I will have to do  
a while bunch of these type of accessor’s in my model. One for each  
field in my seperate polymorphic instances?

def instance\_linkurl  
instance.linkurl  
end

etc…

Will that break when ferret trys to trawl a polymorphic model that  
doesn’t have a linkurl attribute?

> > As a laughing point, when I wrote the original application in PHP using  
> > Mysql fulltext search, the query for the frontend search was 2 pages  
> > long! 🙂
> 
> dont tell me any details 😉
> 
> cheers,  
> Jens
> 
> –  
> webit! Gesellschaft f?e Medien mbH [www.webit.de](http://www.webit.de)  
> Dipl.-Wirtschaftsingenieur Jens Krä­¥r removed\_email\_address@domain.invalid  
> Schnorrstraß¥ 76 Tel +49 351 46766 0  
> D-01069 Dresden Fax +49 351 46766 66

---

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [November 20, 2006, 2:47pm UTC](https://www.ruby-forum.com/t/acts-as-ferret-and-associations/74424/14 "2006-11-20T14:47:27Z")

</div>

On Fri, Nov 17, 2006 at 03:20:43AM +0100, Brendon M. wrote:

> Jens K. wrote:  
> […]
> 
> > ComponentInstance.find\_by\_contents(qp.parse user\_query)  
> > def instance\_linkurl  
> > instance.linkurl  
> > end
> 
> etc…

that depends on what you want to do. If you want fine grained searches  
on single fields like linkurl or name then yes, you’ll have to have  
these accessors. but with ruby you can easily declare them in a  
programmatic way, i.e loop over the attributes of instance and ue  
define\_method inside the loop…

if you don’t need that many fields for querying you also can aggregate  
the values of your instance attributes to a single string and index  
that instead.

> Will that break when ferret trys to trawl a polymorphic model that  
> doesn’t have a linkurl attribute?

it probably will, you’ll have to check if the object has this method and  
return ‘’ if it doesn’t.

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:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [November 20, 2006, 10:44pm UTC](https://www.ruby-forum.com/t/acts-as-ferret-and-associations/74424/15 "2006-11-20T22:44:35Z")

</div>

Thanks for all your help above 🙂

Coming back to the original query (now that I’m back at work to try it),  
when I create a “component instance” i get the following in the logs:

Processing FolderController#new (for 127.0.0.1 at 2006-11-21 10:23:29)  
[POST]  
Session ID: e3154e167b8382c5050468c261bd0ead  
Parameters: {“commit”=\>“Add”, “folder”=\>{“name”=\>“cool folder”},  
“action”=\>“new”, “controller”=\>“admin/construction/folder”,  
“parent\_id”=\>“12”}  
e[4;36;1mFolder Columns (0.016000)e[0m e[0;1mSHOW FIELDS FROM  
folderse[0m  
e[4;35;1mSQL (0.000000)e[0m e[0mBEGINe[0m  
e[4;36;1mSQL (0.046000)e[0m e[0;1mINSERT INTO folders (`name`)  
VALUES(‘cool folder’)e[0m  
e[4;35;1mSQL (0.094000)e[0m e[0mCOMMITe[0m  
e[4;36;1mComponentInstance Columns (0.016000)e[0m e[0;1mSHOW FIELDS  
FROM component\_instancese[0m  
e[4;35;1mComponent Load (0.015000)e[0m e[0mSELECT \* FROM components  
WHERE (technical\_name = ‘folder’) LIMIT 1e[0m  
e[4;36;1mComponent Columns (0.000000)e[0m e[0;1mSHOW FIELDS FROM  
componentse[0m  
e[4;35;1mSQL (0.000000)e[0m e[0mBEGINe[0m  
e[4;36;1mComponentInstance Load (0.000000)e[0m e[0;1mSELECT \* FROM  
component\_instances WHERE (parent\_id) ORDER BY position DESC LIMIT 1e[0m  
e[4;35;1mSQL (0.032000)e[0m e[0mINSERT INTO component\_instances  
(`deleted_root_item`, `instance_type`, `deleted_on`, `enabled`,  
`instance_id`, `component_id`, `parent_id`, `position`) VALUES(NULL,  
‘Folder’, NULL, 1, 4, 2, 12, 5)e[0m  
ferret\_create/update: ComponentInstance : 20  
creating doc for class: ComponentInstance, id: 20  
e[4;36;1mSQL (0.031000)e[0m e[0;1mCOMMITe[0m  
Redirected to  
[http://localhost:3001/admin/construction/construction\_zone/12](http://localhost:3001/admin/construction/construction_zone/12)  
Completed in 1.01600 (0 reqs/sec) | DB: 0.25000 (24%) | 302 Found  
[[http://localhost/admin/construction/folder/12/new](http://localhost/admin/construction/folder/12/new)]

The current model code is:

class ComponentInstance \< ActiveRecord::Base  
has\_many :permissions  
has\_many :groups, :through =\> :permissions  
belongs\_to :component #will be in different database ??  
acts\_as\_tree :order =\> ‘position’  
acts\_as\_list :scope =\> ‘parent\_id’  
belongs\_to :instance, :polymorphic =\> true  
acts\_as\_ferret(  
:fields =\> :instance\_name  
)

def instance\_name  
instance.name  
end

– And here is the controller method to make a new folder  
(component\_instance)

def new  
if request.post?  
@folder = Folder.new(params[:folder])  
success = @folder.save

```
  component_instance = ComponentInstance.new
  component_instance.enabled = 1; #ie True
  component_instance.instance = @folder
  component_instance.parent_id = params[:parent_id]
  component_instance.component = Component.find(:first, :conditions 

```

=\>“technical\_name = ‘folder’”)

```
  if success && component_instance.save
    flash[:notice] = 'A new folder was successfully added.'
    redirect_to :controller => 'construction_zone', :parent_id => 

```

params[:parent\_id]  
else  
@folder.destroy if success #Make sure the link and  
component\_instances tables are consistent  
flash[:fail] = ‘There was an error when saving the folder’  
end  
else  
@folder = Folder.new  
end  
end

Any ideas would be greatly appreciated. 🙂

Brendon

---

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [November 20, 2006, 11:39pm UTC](https://www.ruby-forum.com/t/acts-as-ferret-and-associations/74424/16 "2006-11-20T23:39:30Z")

</div>

Oh so, here’s how to do it 🙂

acts\_as\_ferret :fields =\> [‘instance\_name’]

def instance\_name  
instance.name  
end

Seems to be the correct syntax 🙂 Not sure why the symbol way didn’t do  
it?

Cheers,

Brendon

---

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [November 21, 2006, 10:01am UTC](https://www.ruby-forum.com/t/acts-as-ferret-and-associations/74424/17 "2006-11-21T10:01:57Z")

</div>

On Mon, Nov 20, 2006 at 11:39:30PM +0100, Brendon M. wrote:

> it?  
> imho it’s not the symbol that was the problem, but the missing brackets  
> around it - in your original post you wrote

acts\_as\_ferret :fields =\> :instance\_name

So  
acts\_as\_ferret :fields =\> [:instance\_name]  
should work, too.

Jens

> [http://rubyforge.org/mailman/listinfo/ferret-talk](http://rubyforge.org/mailman/listinfo/ferret-talk)

–  
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:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [September 18, 2007, 6:10am UTC](https://www.ruby-forum.com/t/acts-as-ferret-and-associations/74424/18 "2007-09-18T06:10:38Z")

</div>

I think i answered my own question.  
in the create method for my note i added  
Contact.find(params[:id]).ferret\_create()  
hooha

Adam J. wrote:

> Thanks guys,
> 
> The above seems to work for me perfectly except when i add a new not it  
> doesnt update the index. if i delete and rebuild the index it works  
> fine. can some one stear me in the right direction please i am guesing i  
> have to add some metod to the create function in my notes\_controller to  
> update the index ???

---

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [September 18, 2007, 5:31am UTC](https://www.ruby-forum.com/t/acts-as-ferret-and-associations/74424/19 "2007-09-18T05:31:25Z")

</div>

Thanks guys,

The above seems to work for me perfectly except when i add a new not it  
doesnt update the index. if i delete and rebuild the index it works  
fine. can some one stear me in the right direction please i am guesing i  
have to add some metod to the create function in my notes\_controller to  
update the index ???

Thanks, Adam

## my code in contacts\_controller:

class Contact \< ActiveRecord::Base  
validates\_presence\_of :First\_Name  
validates\_presence\_of :Last\_Name  
has\_many :notes, :as =\> :notable

acts\_as\_ferret :store\_class\_name =\> true, :lazy =\> true, :remote =\>  
false, :fields =\>  
{

```
      :First_Name => { :store => :no, :boost => 2 },
      :Last_Name => { :store => :no, :boost => 1 },
      :description => { :store => :yes, :boost => 0.5 },
      :agent_id => { :store => :no, :boost => 0 },
      :note_note => { :store => :yes, :boost => 0 }

```

}

def note\_note

```
  @index = Array.new
  for note in self.notes
    @index << note.note
  end
  return @index.join(" ")

```

end

---

<div class="post-metadata">

**Author:** ![mjgp2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@mjgp2](https://www.ruby-forum.com/u/mjgp2)\
**Post date:** [September 20, 2007, 10:35pm UTC](https://www.ruby-forum.com/t/acts-as-ferret-and-associations/74424/20 "2007-09-20T22:35:46Z")

</div>

On Tue, Sep 18, 2007 at 06:10:38AM +0200, Adam J. wrote:

> I think i answered my own question.  
> in the create method for my note i added  
> Contact.find(params[:id]).ferret\_create()

right, however I’d stick that code in a NotesObserver…

Jens

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Ferret-talk mailing list  
> removed\_email\_address@domain.invalid  
> [http://rubyforge.org/mailman/listinfo/ferret-talk](http://rubyforge.org/mailman/listinfo/ferret-talk)

–  
Jens Krämer  
[http://www.jkraemer.net/](http://www.jkraemer.net/) - Blog  
[http://www.omdb.org/](http://www.omdb.org/) - The new free film database

[Next page](https://www.ruby-forum.com/t/acts-as-ferret-and-associations/74424.md?page=2)
