This project is archived and is in readonly mode.
has_many with :primary_key option bug
Reported by Roman Shterenzon | December 25th, 2008 @ 07:15 PM | in 2.x
Creating new object through a has_many association which has a :primary_key option doesn't work as expected, as it always assigns the id of the owner instead of using the provided :primary_key option. Attached is the fix with the corresponding unit test.
Comments and changes to this ticket
-
Frederick Cheung December 25th, 2008 @ 09:52 PM
That's odd as foo.id always gets you the value of the primary key (whether it's called id or not) - I'm not sure the change in the patch accomplishes anything.
-
Roman Shterenzon December 25th, 2008 @ 09:59 PM
Perhaps I fixed it in the wrong place, but it surely fixes the bug that I found while working on my application. Since the "primary_key" option is a part of the reflection object, I'm not sure that it's known to the association owner, and the only thing that can change "id" is the 'set_primary_key' for the owner class (but it's something different). Maybe I got it wrong.
-
Repository December 30th, 2008 @ 06:09 PM
- State changed from new to resolved
(from [21efba464afa2ae6e5dfd938ac8a3ce446faf7e7]) Fix HasManyAssociation#create ignoring the :primary_key option [#1633 state:resolved]
Signed-off-by: Frederick Cheung frederick.cheung@gmail.com http://github.com/rails/rails/co...
-
Repository February 2nd, 2009 @ 07:57 PM
(from [7276128fb47d5997437eb5452b4e9a82ee54f4a8]) Fix HasManyAssociation#create ignoring the :primary_key option [#1633 state:resolved]
Signed-off-by: Frederick Cheung frederick.cheung@gmail.com http://github.com/rails/rails/co...
-
Brian Johnson February 2nd, 2009 @ 08:41 PM
This is not completely resolved, please see http://github.com/johnsbrn/rails...
-
Roman Shterenzon February 2nd, 2009 @ 09:36 PM
Brian, I've seen your commit, but I didn't quite understand what is missing in the current fix. Can you elaborate? Or, perhaps, define a failing test.
-
Brian Johnson February 3rd, 2009 @ 03:49 PM
There is still an issue in has_one in associations.rb and new_record in has_one_association.rb. There are some tests included in the commit, but just from looking at the code you can see that it's still using id instead of the primary_key option if it exists.
-
Brian Johnson February 3rd, 2009 @ 03:51 PM
I also made an update to make one of the changes consistent with the existing fix from this ticket.
-
Brian Johnson February 3rd, 2009 @ 03:53 PM
Sorry, just one more thing, this started as ticket 1756 http://rails.lighthouseapp.com/p...
-
Roman Shterenzon February 3rd, 2009 @ 10:17 PM
So, if I get it right, it has nothing to do with 'has_many' association? If this is indeed the case, then this bug can be closed.
-
Brian Johnson February 4th, 2009 @ 01:42 AM
Oops, my bad. Yes, this is for a bug with has_one. I guess I had has_one on my mind so I saw has_one instead of has_many in the title. There is a similar bug in has_one that is addressed by my patch which is in ticket 1756.
-
tribalvibes September 17th, 2010 @ 09:09 AM
- Tag changed from 2.2, has_many, primary_key to 2.3.10, 2.3.8, 2.3.x, has_many, primary_key
This is still a bug in 2.3.8. Seems the fixes have not been integrated?
class Service < ActiveRecord::Base # attributes: pk id, proxy_id, etc. has_many :clients, primary_key: :proxy_id, foreign_key: :proxy_id, before_add: :client_add def client_add( b ) #!!! here b.proxy_id is not set correctly end end p = Service.first.clients.create # p.proxy_id is set to Service.first.id (i.e. the class pk) rather than proxy_id # which is the designated pk for the association
-
Andrea Campi October 16th, 2010 @ 11:34 PM
- Tag changed from 2.3.10, 2.3.8, 2.3.x, has_many, primary_key to 2-3-st, has_many, primary_key
-
Andrea Campi October 16th, 2010 @ 11:34 PM
- Tag changed from 2-3-st, has_many, primary_key to 2-3-stable, has_many, primary_key
-
csnk May 18th, 2011 @ 08:22 AM
We are the professional sweaters manufacturer,sweaters supplier, sweaters factory, custom sweaters.
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
- 1633 has_many with :primary_key option bug (from [21efba464afa2ae6e5dfd938ac8a3ce446faf7e7]) Fix Has...
- 1756 has_one with :foreign_key & :primary_key bug Similar to #1633
- 1633 has_many with :primary_key option bug (from [7276128fb47d5997437eb5452b4e9a82ee54f4a8]) Fix Has...
- 1756 has_one with :foreign_key & :primary_key bug Similar to #1633 which is still a bug in 2.3.8 has_many a...