Ruby on Rails ActiveRecord: eager loading issue with foreign and primary key

Posted by Krishnaswamy Subramanian on Stack Overflow See other posts from Stack Overflow or by Krishnaswamy Subramanian
Published on 2010-04-29T10:44:57Z Indexed on 2010/04/29 11:27 UTC
Read the original article Hit count: 367

Filed under:

The eager loading on Ruby on Rails is not working properly for the following scenario.

First we had a model called marks which has the following fields
id, student, subject, mark

the student is a string column which has the active directory login value, later on for reporting functionality we introduce another table called user which has the following fields
id, ad_name, full_name

Now on the Mark model, we have added the belongs to class

belongs_to :student_details, :class_name => "User", :foreign_key => "student", :primary_key => "ad_name"

and when loading using the ActiveRecord's find method we are passing in the include conditon for eager loading

Marks.find(:all, :include => :reserved_user)

but when the find is executed, for each and every mark a student select query executed.

Is this a known bug in ROR? or am i missing something?

© Stack Overflow or respective owner

Related posts about ruby-on-rails