问题
I have a model called User which has_one
Player. A Player belongs_to
a User.
I want to find all the Players which Users attributes City has a particular value. Right now I have this in my Player model:
def find
User.find(:all, :conditions => ['city LIKE ?', "%#{city}%"])
end
However that gives me the User. I want the Players which Users satisfy that condition.
How do I do that?
回答1:
Try this.
Player.joins(:user).where('user.city LIKE ?', "%#{city}%")
来源:https://stackoverflow.com/questions/11337543/rails-search-in-has-one-association