This project is archived and is in readonly mode.

#2960 ✓wontfix
Peter Leonhardt

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

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>

Attachments

Pages