Rails: Search in has_one association

北城以北 提交于 2020-01-06 02:38:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!