This project is archived and is in readonly mode.
find creates wrong SQL when you set "has_one :through" association on :join option
Reported by Tatsuya Ono | January 11th, 2010 @ 11:33 PM
I ran into an issue on has_one :through.
First patch(test_join_has_many_throgh.diff) adds test cases to reproduce the issue.
When you define an association like the following ...
class Author
has_many :author
end
class Post
belongs_to :author
end
class Comment
belongs_to :post
has_one :post_author, :through=>:post, :source=>:author
end
And if you write the code like the following ...
comments = Comment.find :all, :conditions=>["authors.name = ?", "David"], :joins=>[:post_author]
It creates a SQL like this and you will get an
exception.
SELECT "comments".* FROM "comments"
INNER JOIN "posts" ON ("comments"."id" = "posts"."post_id") # <= Here is the problem
INNER JOIN "authors" ON ("authors"."id" = "posts"."author_id")
WHERE (authors.name = 'David')
Since through_reflection is not :has_many but :belongs_to, the second line should be comments.post_id=posts.id.
The second patch(fix_join_has_one_through.diff) fixes this issue; it checks through_reflection.macro and creates a correct SQL in association.rb. It also includes test cases defined on the first patch and I checked the tests are passed as well as other existing tests.
(The patches are made against 2-3-stable.)
Comments and changes to this ticket
-
Tatsuya Ono January 11th, 2010 @ 11:35 PM
- Title changed from invalid SQL is created when you set "has_one :through" association to :join to find creates wrong SQL when you set "has_one :through" association on :join option
-
Tatsuya Ono January 11th, 2010 @ 11:54 PM
- Tag changed from activerecord associations, activerecord to activerecord associations, 2.3.5, activerecord
-
Paul Bowsher January 12th, 2010 @ 08:06 AM
This patch will probably need to be forward-ported to ARel too
-
Michael Koziarski January 16th, 2010 @ 02:34 AM
- Assigned user set to Pratik
-
Tatsuya Ono February 4th, 2010 @ 01:21 AM
- Tag changed from activerecord associations, 2.3.5, activerecord to 2.3.5, 2.3.6, activerecord, associations
I upload a patch which does not contain unnecessary spaces.
This patch will probably need to be forward-ported to ARel too
I don't think that ARel itself is having problem related to this, though, I will make a patch against master. Thanks for the suggestion.
-
Ryan Bigg October 11th, 2010 @ 12:11 PM
- Tag cleared.
- Importance changed from to Low
Automatic cleanup of spam.
-
Jon Leighton December 19th, 2010 @ 05:05 PM
- Assigned user changed from Pratik to Aaron Patterson
-
Jon Leighton December 20th, 2010 @ 08:07 PM
- State changed from new to duplicate
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
Attachments
Referenced by
- 3846 [PATCH][3.0] join doesn't work properly for association with :through This is a same issue as the following. https://rails.ligh...