假设我有ActiveRecord模型A,B和C:
class A < ActiveRecord::Base
belongs_to :c
has_one :b
end
class B < ActiveRecord::Base
belongs_to :c
end
class C < ActiveRecord::Base
has_many :a
has_one :c
end
数据库表格如下:
Table a:
id (auto increment) | b_id | c_id
Table b:
id (auto increment) | c_id
Table c:
id (auto increment)
现在我想要,
SELECT a.id, b.id, c.id, b_c.id
FROM a
INNER JOIN b ON a.b_id = b.id
INNER JOIN c ON a.c_id = c.id
INNER JOIN c b_c b.c_id = b_c.id
我知道我可以直接在joins
和select
使用SQL来实现:
A.joins(:b, :c).joins('INNER JOIN c b_c b.c_id = b_c.id')
有轨道方式吗? 他可以使用类似Rail的语法吗?类似的东西:
A.joins(:c, :b => :c).select(:c => :id).select(:b => { :c => :id})