Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Possible Bug with Eager Loading (github sample included)

#6088

I'm having problems with eager loading. I've posted a sample project here:

https://github.com/rdetert/Rails-Test/tree/master/eager_loading/

From the README:

I'm using an alternative to the built-in polymorphism to take advantage of DB level integrity.

Assume another table called posts that has a column, posts.commentable_id

I AM TRYING TO EAGERLY LOAD ALL COMMENTS GIVEN TO A POST TOPIC, INCLUDING USER INFO AND WHETHER OR NOT THEY HAVE VOTED ON THE COMMENT.

To Install:

$ rake db:migrate
$ rake db:seed

$ rails c
ruby-1.8.7-p299 > Comment.with_eager_votes(User.first).with_eager_userdata.where(:commentable_id => 1).to_sql

 => "SELECT * FROM \"comments\" INNER JOIN \"votable\" ON \"votable\".\"id\" = \"comments\".\"votable_id\" INNER JOIN \"users\" ON \"users\".\"id\" = \"comments\".\"user_id\" LEFT OUTER JOIN votes ON votes.votable_id = votable.id AND votes.user_id=1 WHERE (\"comments\".\"commentable_id\" = 1)"

This is the correct query and returns the correct data set
However, when votes are null, it does not behaves as expected. The eagerly loaded associations are also null.

IN THIS RESULT SET, THE USER HAS NOT VOTED ONLY ON THE FIRST COMMENT.

ruby-1.8.7-p299 > @comments = Comment.with_eager_votes(User.first).with_eager_userdata.where(:commentable_id => 1)

ruby-1.8.7-p299 > @comments.first.user       # => WRONG
 => nil 

ruby-1.8.7-p299 > @comments.second.user
 => #<User id: 1, name: "Big Chief", created_at: "2010-11-30 10:51:19", updated_at: "2010-11-30 10:51:19"> 

ruby-1.8.7-p299 > @comments.third.user
 => #<User id: 1, name: "Big Chief", created_at: "2010-11-30 10:51:19", updated_at: "2010-11-30 10:51:19"> 

ruby-1.8.7-p299 >

Reported by Ryan Detert · November 30th, 2010 @ 11:26 AM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. rails
    rails
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    March 29th, 2011 @ 01:00 AM

  2. rails
    rails
    • State changed from open to stale

    March 29th, 2011 @ 01:00 AM