Find_with_ferret and HABTM assocs

Hi there, I have now read a lot of pages on doing find_with_ferret with
associations, and still can’t grasp how to solve my specific situation.
I might be dense when not seeing how to apply the info I have read to my
situation, so I will try to start this topic in hope that it might also
be usefuls for others in the same situation:

I have user objects with roles so my user object has:
hasandbelongstomany :roles
while my role object has
hasandbelongstomany :users

I thought then that I could do a :
acts_as_ferret :fields => [:name, :role_name], :remote => false

and:

def role_name
self.roles.collect{ |role| role.name}.join(" ")
end

and then do the following:

find_with_ferret “#{q}”, :page => page, :per_page => 10,:order =>
‘name’, :conditions => [“role_name = #{role}”]

I have also tried many other constellations with multiple and include
and not getting it right because I did not see how to apply it to my
situation.

Does anyone have a suggestion ?

Could you be more specific about what is not working?
– Are your association methods between Role and User being set up
correctly? (If not, make sure your statement is
“has_and_belongs_to_many”, and not “hasandbelongstomany”.)
– Are you storing the data for role_name in the index? If so, can
you see it there?

I’m not familiar with the find_with_ferret method (because I use
index.search_each). However, in my code I do have a case where I do
something like your role_name method, and the output gets indexed.

–Paul

Rolf Guescini wrote:

Hi there, I have now read a lot of pages on doing find_with_ferret with
associations, and still can’t grasp how to solve my specific situation.
I might be dense when not seeing how to apply the info I have read to my
situation, so I will try to start this topic in hope that it might also
be usefuls for others in the same situation:

I have user objects with roles so my user object has:
hasandbelongstomany :roles
while my role object has
hasandbelongstomany :users

I thought then that I could do a :
acts_as_ferret :fields => [:name, :role_name], :remote => false

and:

def role_name
self.roles.collect{ |role| role.name}.join(" ")
end

and then do the following:

find_with_ferret “#{q}”, :page => page, :per_page => 10,:order =>
‘name’, :conditions => [“role_name = #{role}”]

I have also tried many other constellations with multiple and include
and not getting it right because I did not see how to apply it to my
situation.

Does anyone have a suggestion ?

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