This project is archived and is in readonly mode.
belongs_to with :primary_key does not work with find's :include
Reported by Tomash | September 15th, 2009 @ 11:01 AM | in 2.3.9
Since 2.3.3 Rails supports both :foreign_key and :primary_key
for belongs_to association.
Using both (or only :primary_key) works great for simple
instance.owner.
However, when using Owned.find(..., :include => [:owner]) the
owner gets joined using it's ID (model's own primary key) column
instead of the :primary_key defined within Owned's (which is the
expected behaviour) belongs_to :owner association.
Thus
owned.owner yields different results based on whether owned was
pulled using Owned.find(id, :include => [:owner]) or
Owned.find(id) (and of course .owner gets pulled when owned.owner
gets called in the view).
It was pretty hard to stumble upon this error, as one needs to
have some overlapping values in both columns. In our case it
was
regions table has column "zip" (5-digit numbers)
users table has column "postal_code", User belongs_to(:region,
:foreign_key => :postal_code, :primary_key => :zip)
It turned out that when using @user=User.find(id, :include =>
[:region]) the region was joined using regions.id instead of
regions.zip:
SELECT "users".* FROM "users" LEFT OUTER JOIN "regions" ON "regions".id = "users".postal_code WHERE ("users"."id" = 1))
Comments and changes to this ticket
-
CancelProfileIsBroken September 25th, 2009 @ 12:09 PM
- Tag changed from 2.3.3, :include, associations, belongs_to to 2.3.3, :include, associations, belongs_to, bugmash
-
Matt Mitchell September 29th, 2009 @ 07:07 PM
I'm uploading a patch here. The JoinAssociation.associtation_join method now first checks for the reflection.options[:primary_key] and then defaults to reflection.klass.primary_key if the option is missing. This is my first patch so please let me know if this isn't the way to do it.
-
Deleted User December 15th, 2009 @ 08:24 AM
- Tag changed from 2.3.3, :include, associations, belongs_to, bugmash to 2, :include, associations, belongs_to, bugmash
I'm using Rails 2.3.4, and I'm seeing this behaviour too.
-
Yaroslav Markin February 11th, 2010 @ 12:44 PM
- Assigned user set to Frederick Cheung
Rails 2.3.5, before the patch I was having horrible problems with ThinkingSphinx indexing belongs_to association with :primary_key. Applied it, works just fine.
-
Rizwan Reza February 12th, 2010 @ 12:46 PM
- Tag changed from 2, :include, associations, belongs_to, bugmash to 2, :include, associations, belongs_to
-
Yaroslav Markin April 12th, 2010 @ 04:20 PM
- Tag changed from 2, :include, associations, belongs_to to 2, :include, activerecord, associations, belongs_to, bug, patch
- Assigned user changed from Frederick Cheung to José Valim
Requesting committer attention :)
-
Jeremy Kemper April 24th, 2010 @ 10:27 PM
- Milestone set to 2.3.6
- State changed from new to incomplete
Needs a test.
-
José Valim April 29th, 2010 @ 11:53 AM
- Assigned user changed from José Valim to Jeremy Kemper
-
José Valim April 30th, 2010 @ 03:17 PM
- Tag changed from 2, :include, activerecord, associations, belongs_to, bug, patch to 2, :include, activerecord, associations, belongs_to, bug, bugmash, patch
-
Łukasz Strzałkowski May 5th, 2010 @ 07:38 PM
Jeremy, isn't that fixed and tested with this commit?
http://github.com/rails/rails/commit/00b95eb265ff606ae0a7b650265aa3...
Personally I think the test is week.
-
Ernie Miller May 5th, 2010 @ 11:35 PM
- Tag changed from 2, :include, activerecord, associations, belongs_to, bug, bugmash, patch to 2, :include, activerecord, associations, belongs_to, bug, bugmash, patch, tests
-
HBpencil May 16th, 2010 @ 07:12 AM
- Assigned user cleared.
try this
class Region
set_primary_key :zip end -
Anil Wadghule May 16th, 2010 @ 12:24 PM
+1
Verified both patches of tests on rails 3 master and 2-3 stable. HBpencil what are you trying to say? If possible more tests would be good.
-
Rizwan Reza May 16th, 2010 @ 04:59 PM
- Tag changed from 2, :include, activerecord, associations, belongs_to, bug, bugmash, patch, tests to 2, :include, activerecord, associations, belongs_to, bug, bugmash-review, patch, tests
- State changed from incomplete to verified
-
Łukasz Strzałkowski June 28th, 2010 @ 06:58 PM
- Importance changed from to Medium
This ticket actually can be closed. It's duplicate of https://rails.lighthouseapp.com/projects/8994/tickets/765
and this is solved by this commit: http://github.com/rails/rails/commit/63026541b209cc11ffd74cf3ca04b8...
-
Andrea Campi October 16th, 2010 @ 11:40 PM
- Tag changed from 2, :include, activerecord, associations, belongs_to, bug, bugmash-review, patch, tests to 2-3-stable, :include, activerecord, associations, belongs_to, bug, bugmash-review, patch, tests
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>