This project is archived and is in readonly mode.

#2790 ✓invalid
johnny

ActiveRecord :include doesn't work with foreign keys

Reported by johnny | June 11th, 2009 @ 03:04 PM | in 2.x

When creating a belongs_to association in an ActiveRecord model it's possible to have the model eager load the associated model. For example:

class Student < ActiveRecord::Base

belongs_to :lecturer

end

Using: Student.find(:all, :include => :lecturer) - should bring all the students and their lecturers, resulting in a query like "SELECT * from lecturers WHERE id IN (1, 2, 3)".

However, if we use a foreign key like so:

class Student < ActiveRecord::Base

belongs_to :lecturer, :foreign_key => :some_key

end

Using the above find will eager load only the lecturer for the first student. The rest will be lazy loaded.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Pages