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

QueXuQ · May 22, 2014 · Last by tumayun replied at May 22, 2014 · 1528 hits

假设有

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...

You need to Sign in before reply, if you don't have an account, please Sign up first.