This project is archived and is in readonly mode.
has_many :through associations give different results depending on eager-loading via :include
Reported by David Lee | April 7th, 2009 @ 11:40 PM | in 3.x
class User
has_many :comments
has_many :posts, :through => :comments
end
class Comment
belongs_to :user
belongs_to :post
end
x = User.create
x.comments.create :post_id => nil
x.posts #=> []
x = User.find :first
x.posts #=> []
x = User.find :first, :include => [:posts]
x.posts #=> [nil] !!!
I'm not sure if posts should return [] or [nil], but the result should be consistent regardless of whether the association was eager-loaded or not.
Comments and changes to this ticket
-
Tatsuya Ono June 16th, 2009 @ 01:40 AM
- no changes were found...
-
Tatsuya Ono June 16th, 2009 @ 01:40 AM
I wrote a patch for this issue against 2-3-stable.
I also put a simple project on github which allows you to reproduce this issue easily.
http://github.com/ono/eager_load_issue/tree/masterClone it then run rake test. It will be failed despite for it should be passed.
# sets up data u1 = User.find :all u2 = User.find :all, :include=>['posts_commented'] assert_equal u1[0].posts_commented.size, u2[0].posts_commented.size
You will succeed to pass the test if you apply the patch attached.
-
Jon Leighton December 16th, 2010 @ 12:35 AM
- Assigned user set to Aaron Patterson
- Importance changed from to
Can this ticket be closed or at least marked as stale please? If you look at
association_preload.rb
in current master, you will see that it doesArray.wrap(associated_record)
, which deals with the case ofassociated_record
being nil, which is the source of the problem reported. -
Aaron Patterson December 16th, 2010 @ 01:46 AM
- State changed from new to stale
@Jon yes. Marking as stale. :-)
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>