2

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})
2
  • Not able to follow your question. Can you explain it a little bit more? Commented Jul 29, 2015 at 17:26
  • Edited. Is the question clear now? Commented Jul 30, 2015 at 13:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.