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.

has_many with :primary_key option bug

#1633

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.

Reported by Roman Shterenzon · December 25th, 2008 @ 07:15 PM

State: resolved
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Frederick Cheung
    Frederick Cheung

    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.

    December 25th, 2008 @ 09:52 PM

  2. Roman Shterenzon
    Roman Shterenzon

    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.

    December 25th, 2008 @ 09:59 PM

  3. Frederick Cheung
    Frederick Cheung

    D'oh - read a little too quickly !

    December 25th, 2008 @ 10:31 PM

  4. Repository
    Repository
    • State changed from new to resolved

    (from [21efba464afa2ae6e5dfd938ac8a3ce446faf7e7]) Fix HasManyAssociation#create ignoring the :primary_key option [#1633 has_many with :primary_key option bug state:resolved]

    Signed-off-by: Frederick Cheung frederick.cheung@gmail.com http://github.com/rails/rails/co...

    December 30th, 2008 @ 06:09 PM

  5. Roman Shterenzon
    Roman Shterenzon

    Is it possible to merge this to 2-2-stable branch?

    January 4th, 2009 @ 03:42 PM

  6. Repository
    Repository

    (from [7276128fb47d5997437eb5452b4e9a82ee54f4a8]) Fix HasManyAssociation#create ignoring the :primary_key option [#1633 has_many with :primary_key option bug state:resolved]

    Signed-off-by: Frederick Cheung frederick.cheung@gmail.com http://github.com/rails/rails/co...

    February 2nd, 2009 @ 07:57 PM

  7. Brian Johnson
  8. Roman Shterenzon
    Roman Shterenzon

    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.

    February 2nd, 2009 @ 09:36 PM

  9. Brian Johnson
    Brian Johnson

    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.

    February 3rd, 2009 @ 03:49 PM

  10. Brian Johnson
    Brian Johnson

    I also made an update to make one of the changes consistent with the existing fix from this ticket.

    February 3rd, 2009 @ 03:51 PM

  11. Brian Johnson
    Brian Johnson

    Sorry, just one more thing, this started as ticket 1756 http://rails.lighthouseapp.com/p...

    February 3rd, 2009 @ 03:53 PM

  12. Roman Shterenzon
    Roman Shterenzon

    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.

    February 3rd, 2009 @ 10:17 PM

  13. Brian Johnson
    Brian Johnson

    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.

    February 4th, 2009 @ 01:42 AM

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

    September 17th, 2010 @ 09:09 AM

  15. Andrea Campi
    Andrea Campi
    • Tag changed from 2.3.10, 2.3.8, 2.3.x, has_many, primary_key to 2-3-st, has_many, primary_key

    October 16th, 2010 @ 11:34 PM

  16. Andrea Campi
    Andrea Campi
    • Tag changed from 2-3-st, has_many, primary_key to 2-3-stable, has_many, primary_key

    October 16th, 2010 @ 11:34 PM