Acts_as_ferret with has_many relationship

Using Ferret and the acts_as_ferret plugin, I would like to retrieve
results based on models descended from a parent model:

class Post < ActiveRecord::Base

acts_as_ferret :fields => [ 'title',
			'body',
			:post_author,
                            :post_comments
			]

has_one :author
has_many :comments
end

I would like to search all comments of a post and retrieve Post models
as search results. Currently, searching across :author fields is
achieved using:

def post_author
self.author.name
end

However, a similiar technique does not seem to work for the has_many
relationship:

def post_comments
for comment in self.comments
@comments << comment.body
@comments << comment.name
end
@comments.join(" ")
end

Searching across author fields finds the correct Post. However, none of
the comments seems to be indexed and/or associated with the correct Post
so no search results are found. I would appreciate any help on this.

Thanks!

Rami


Note: The post model is saved after comment changes are made in order
for acts_as_ferret to index the Post. Also note that multi_search would
not easily apply here since I know for certain I would like to retrieve
the Post model, and not the Comment or Author model as search results.

Hi Rami,

could you have a look at your development.log and check what values
actuelly gets indexed for the post_comments field when you save the
post ?

Jens

On Fri, Jul 21, 2006 at 08:51:48AM +0200, Rami Bitar wrote:


self.author.name
@comments.join(" ")

http://rubyforge.org/mailman/listinfo/ferret-talk

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

You might want to try removing the @ symbol from comments and name it
something else to avoid conflict with your relationship.

def post_comments
self.comments.inject([]) { |ary, c| ary << c.body << c.name }.join(’
')
end

-Lee

Hi Jens,

After looking at the development log, I’ve solved the problem. It turns
out that acts_as_ferret was not indexing ‘comments’ since another table
I was also trying to index was ‘null.’ As a result, acts_as_ferret was
terminating prematurely (due to the null error) before it could even
index the ‘comment’ values. The code I originally posted, therefore,
should work fine for others.

Thanks for your help and all your great work!

Rami

@Lee:
Yes, thanks for the suggestion. I will remove the @ symbol to eliminate
any possible conflicts.

Jens K. wrote:

Hi Rami,

could you have a look at your development.log and check what values
actuelly gets indexed for the post_comments field when you save the
post ?

Jens

On Fri, Jul 21, 2006 at 08:51:48AM +0200, Rami Bitar wrote:


self.author.name
@comments.join(" ")

http://rubyforge.org/mailman/listinfo/ferret-talk

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

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