Spidy
Spidy

Reputation: 40002

Rails Find and Include doesn't load a model

I'm searching for users that match a set of tags. I want to return the tags matched and their linked profile. I can get the users with their linked tags, but not the profile. I've tried adding the :profile to the :include, but that doesn't work. I've tried putting the :profile in the :join, which works but only returns the first row.

Any suggestions?

# Returns only one record instead of the four it should
User.find(:all, :joins => :profile, :include => :skills, :conditions => { :tags => { :id => search_tags } } )

# Returns all four records that match but not their profiles
User.find(:all, :include => [:skills, :profile], :conditions => { :tags => { :id => search_tags } } )

Update It turns out my data seeder didn't have Facebook data seeded for all users. So the second find actually works

Upvotes: 0

Views: 235

Answers (2)

dexter
dexter

Reputation: 13583

Please try this

User.where(:tags => { :id => search_tags }).includes([:skills, :profile])

Upvotes: 1

Giorgio Previtera
Giorgio Previtera

Reputation: 383

If your models are linked you don't need to get the profiles with the find method. Once you had the Users you can always call @user.profile

Upvotes: 0

Related Questions