This project is archived and is in readonly mode.

#6088 ✓stale
Ryan Detert

Possible Bug with Eager Loading (github sample included)

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

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 >

Comments and changes to this ticket

  • rails

    rails March 29th, 2011 @ 01:00 AM

    • 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.

  • rails

    rails March 29th, 2011 @ 01:00 AM

    • State changed from “open” to “stale”
  • klkk

    klkk May 23rd, 2011 @ 02:53 AM

    louisvuittonwarehouse.com

  • klkk

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>

People watching this ticket

Pages