Let's say I have ActiveRecord models A, B and 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
The database tables look like:
Table a:
id (auto increment) | b_id | c_id
Table b:
id (auto increment) | c_id
Table c:
id (auto increment)
Now I want,
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
I know I can do it by using SQL directly in joins and select:
A.joins(:b, :c).joins('INNER JOIN c b_c b.c_id = b_c.id')
Is there a rails way to do it? Can his be in Rail-like syntax? Something like:
A.joins(:c, :b => :c).select(:c => :id).select(:b => { :c => :id})