新手问题 嵌套两层的 where 搜索应该怎么写呢?

QueXuQ · 2014年05月22日 · 最后由 tumayun 回复于 2014年05月22日 · 1528 次阅读

假设有

a belongs_to b
b belongs_to c

a has_one b
b has_one c

如果通过 b 的 name 搜索 a,可以这样写:

A.includes(:b).where("bs.name = xxx')

可是我要通过 c 的信息找到 a 的话,应该怎么写呢?includes 两层?

has_one + through 的方式

a has_one b
b has_one c
a has_one c through b
A.joins(:c).where...

A.joins(b: :c).where...

需要 登录 后方可回复, 如果你还没有账号请 注册新账号