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.

Add has_many :primary_key option

#292

Has many associations currently force you to use the id of the owner object as the primary key for the association. This patch adds a :primary_key option to has_many that allows you to specify the name of the method that will return the primary key for the association. The new option is tested and documented as an option on the has_many method.

Reported by André Arko · June 1st, 2008 @ 05:09 PM

State: resolved
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Brian Donovan
    Brian Donovan

    There's a good reason for doing this as opposed to setting the primary key for the class, and that's in cases where there is a generated key that is used to join two tables together.

    June 1st, 2008 @ 05:14 PM

  2. Brad Greenlee
    Brad Greenlee

    Attached patch adds support for :primary_key option for has_one associations as well.

    June 3rd, 2008 @ 06:07 AM

  3. Michael Koziarski
    Michael Koziarski
    • Assigned user set to Pratik

    June 3rd, 2008 @ 11:43 PM

  4. Brad Greenlee
    Brad Greenlee

    John Hume noted on Rails-Core that :primary_key might be confusing and suggested :alternate_key. I think just :key might suffice. Thoughts?

    June 4th, 2008 @ 04:14 PM

  5. André Arko
  6. Pratik
    Pratik
    • State changed from new to invalid

    I don't believe this is a very common requirement. Please consider making a plugin. And well, if there are enough people asking for this feature, we can always get it in.

    Thanks.

    June 4th, 2008 @ 10:30 PM

  7. Brad Greenlee
    Brad Greenlee
    • State changed from invalid to new

    I agree that it isn't a very common requirement, but it is fairly core functionality, and monkeypatching it into AR with a plugin is brittle.

    June 4th, 2008 @ 10:30 PM

  8. Brian Donovan
    Brian Donovan

    I think that either :key or :alternate_key would be good.

    Also, I agree with Brad about monkeypatching this into AR. ActiveRecord does a poor job of making itself configurable (via runtime options) and extensible (via code to provide more features). You only have to look at ActiveRecord::Base.find to see that.

    Not only does this patch add a feature that, while you may not need it, seems like something that should be a configuration option, it also abstracts the association's knowledge of how to get the quoted_id, something that is very important if one were to write a plugin to add this functionality.

    As an aside, what is up with Lighthouse's spam filter? It's flagging comments that also change the state of the ticket made by the person who owns the ticket.

    June 4th, 2008 @ 07:28 PM

  9. André Arko
    André Arko

    Other people do want this. Here is a thread from the rubyonrails list where someone wants to know how to set the key on a has_one association:

    http://groups.google.com/group/r...

    Without quoted_id abstracted out, a plugin would be extremely brittle, and it would probably have to be rewritten whenever the rails association code changed.

    I really think this is a good option to have, since it makes associations much more flexible with very little complexity.

    June 4th, 2008 @ 10:26 PM

  10. Brian Donovan
    Brian Donovan

    Can an admin please go to /spams and mark the above comments as ham?

    June 4th, 2008 @ 10:26 PM

  11. Pratik
    Pratik

    I think it's a good idea to make AR code more extensible such that plugins like this are not very brittle.

    Not sure what's up with LH.

    June 4th, 2008 @ 10:26 PM

  12. André Arko
    André Arko

    If you'd like, I could make another patch that just abstracts out quoted_id so that I could just overwrite that in my plugin.

    Are you any more inclined to include this feature since other people have been asking for it on the rails list?

    June 4th, 2008 @ 10:26 PM

  13. Pratik
    Pratik

    I think we can abstract quoted_id to start with. And include the feature in core if more people asks for it.

    Thanks.

    June 4th, 2008 @ 09:17 PM

  14. André Arko
    André Arko

    I've attached a patch that abstracts out @owner.quoted_id to a method named owner_quoted_id. All of the tests still pass. Thanks.

    June 4th, 2008 @ 10:28 PM

  15. André Arko
    André Arko
    • Tag set to activerecord, has_many, patch, tested

    I've provided a patch just like you asked, and waited three weeks. Is it ever going to be applied, so that I can make a plugin out of my earlier patch? Thanks.

    June 25th, 2008 @ 07:23 PM

  16. Jeremy Kemper
    Jeremy Kemper
    • State changed from new to open
    • Milestone cleared.

    June 26th, 2008 @ 03:00 AM

  17. Jeremy Kemper
    Jeremy Kemper

    Sorry for the wait.

    I think the :primary_key option is great for denormalization and for legacy databases, also.

    June 26th, 2008 @ 03:04 AM

  18. Repository
    Repository
    • State changed from open to committed

    (from [0b12da44aa35b643b17ab1b61634ff952993e357]) Extract owner_quoted_id so it can be overridden. [#292 state:committed]

    http://github.com/rails/rails/co...

    June 26th, 2008 @ 03:06 AM

  19. Jeremy Kemper
    Jeremy Kemper

    Could you rebase the :primary_key patch against latest master?

    June 26th, 2008 @ 03:29 AM

  20. André Arko
    André Arko

    Okay, I've rebased my and Brad's patches against master, and included both patches in the attached 0001-primary-key-option.patch. Please let me know if there's anything else you need to get this applied. Thanks!

    July 2nd, 2008 @ 06:32 AM

  21. André Arko
    André Arko
    • Assigned user changed from Pratik to Jeremy Kemper

    Hmm. Should I create a new ticked for the rebased patch, since this ticket is now marked "committed"?

    July 7th, 2008 @ 05:43 PM

  22. Michael Koziarski
    Michael Koziarski

    It's actually been committed now :)

    July 7th, 2008 @ 06:07 PM

  23. Pratik
    Pratik
    • State changed from committed to resolved

    July 14th, 2008 @ 01:47 AM

  24. Jeremy Kemper
    Jeremy Kemper

    I didn't resolve it earlier since the other patch is still open. indirect, could you open a new ticket and rebase against latest master?

    July 28th, 2008 @ 09:50 AM

  25. André Arko
    André Arko

    Hi Jeremy... all the patches on this ticket have been committed. I rebased a few weeks ago for koz:

    http://github.com/rails/rails/co...

    Thanks for following up, though. :)

    July 28th, 2008 @ 09:58 AM

  26. Jeremy Kemper
  27. Shawn
    Shawn
    • Assigned user cleared.

    Could some one add this :primary_key option into belongs_to in a similar fashion? This option is a nice-to-have for has_many, because you can always use finder_sql to workaround it. But it is a must-to-have for belongs_to because there is no way to wrok around it. I had a quick look at the source code and added

    def owner_quoted_id

    if @reflection.options:primary_key]

    quote_value(@owner.send @reflection.options:primary_key]))

    else

    @owner.quoted_id

    end

    end

    into belongs_to.rb. didn't work

    August 5th, 2008 @ 10:08 PM

  28. logan
    logan

    Just wanted to add some additional motivation for this patch. I'm using uuids in a multi-machine, multi-database configuration with ActiveResource. Since objects are moved between databases on each machine, they can't use the autoincrementing 'id' column, hence uuids. Changing the database primary key to be a uuid has a negative impact on Innodb performance so this patch is the right solution.

    Since all sides of the association are impacted (has_many, belongs_to), the ideal solution would be to specify the primary key as an option on each association declaration, and avoid the finder_sql syntax, which doesn't work on the belongs_to anyway. Of course the same issue exists with has_and_belongs_to_many.

    September 3rd, 2008 @ 08:20 PM

  29. André Arko
    André Arko

    Logan,

    I don't think any additional motivation is needed, since the patch has already been applied to rails. If there are further changes to associations that you would like to make (like adding support for belongs_to :primary_key), a patch would be great. :)

    September 3rd, 2008 @ 09:43 PM

  30. Paul Horsfall
    Paul Horsfall

    Perfect, this is just what I needed. Thanks!

    October 15th, 2008 @ 04:30 PM