views:

345

answers:

2

Hi all,

I am trying to get ActiveRecord to perform the following query:

SELECT A.*, B.*, C.* FROM A INNER JOIN B ON B.ID = B_ID INNER JOIN C ON C.ID = C_ID

The dataset is rather large, and I need as the best performance, hence this specific query.

I have my models and query as follows:

class A < ActiveRecord::Base
  belongs_to :b
  belongs_to :c
end

A.find :all, :include => [:b, :c], :joins => [:b, :c]

However this results in the following queries performed:

SELECT A FROM A INNER JOIN B ON B.ID = B_ID INNER JOIN C ON C.ID = C_ID
SELECT * FROM B WHERE ID IN (...)
SELECT * FROM C WHERE ID IN (...)

Is there any way to make ActiveRecord cleverer and not do the second two queries?

Regards,

Luca Spiller

+1  A: 

That's only a supposition. But I don't see why you need to define the :include and the :joins. Only the :joins should be enough to make the query and might solve your problem.

Damien MATHIEU
Thanks for your reply. Unfortunately, if I remove the :include it doesn't do eager loading which is a requirement.
Luca Spiller