Rails:在has_one关联中搜索 [英] Rails: Search in has_one association

查看:82
本文介绍了Rails:在has_one关联中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 User 的模型,该模型为has_one Player . 播放器 belongs_to一个用户.

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.

我该怎么做?

推荐答案

尝试一下.

Player.joins(:user).where('user.city LIKE ?', "%#{city}%")

这篇关于Rails:在has_one关联中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆