This project is archived and is in readonly mode.
uniq bug on array of ActiveRecord Objects
Reported by Peter Leonhardt | July 27th, 2009 @ 09:41 PM
The uniq method on collections of ActiveRecord Objects relies on the primary_key of the model to determine if two objects are the same.
This can lead to problems if the table does not have an 'id' column, and set_primary_key is not defined, or if the table uses a composite key.
This really boils down to a problem with ==, but manifests in [].uniq
For example,
If you have a table "items" without a column 'id'
Item.create(:some_id => 1, :name => "Item 1")
Item.create(:some_id => 2, :name => "Item 2")
a = Item.find_by_some_id(1)
b = Item.find_by_some_id(2)
a == b #=> true, expected FALSE
In most cases, it is a simple case of utilizing 'set_primary_key', however, when one is interfacing with legacy databases with composite keys? It is also unexpected behavior. There is nothing similar between the two objects besides that they are the same class.
I'm not sure what the proper solution would be. If the primary_key doesn't match any columns, compare values of the attributes? I'll leave that up to debate.
Demo Attached, just rake db:migrate
Comments and changes to this ticket
-
Michael Koziarski August 3rd, 2009 @ 06:02 AM
- Tag changed from active_record, array to active_record, array, bugmash
-
Cristi Balan August 8th, 2009 @ 12:08 PM
-1
Since composite keys are not part of AR core, but added through plugins, I think the equality of records should be handled in the same plugin as well by overriding ==.
-
John Pignata August 9th, 2009 @ 05:58 AM
-1
Where composite keys or Rails-known primary keys aren't used, == should be overriden.
-
Kieran P August 9th, 2009 @ 06:22 AM
-1 In agreement with Cristi and Johns comments. If you're needing this, overwrite the functionality in your own code since its a limited case.
-
Rizwan Reza August 9th, 2009 @ 11:24 PM
not reproducible
-1 Agreed with Cristi... it's a job of a plugin.
-
Jeremy Kemper August 9th, 2009 @ 11:26 PM
- State changed from new to wontfix
- Tag changed from active_record, array, bugmash to active_record, array
- Milestone cleared.
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>