This project is archived and is in readonly mode.

#3491 ✓committed
Gabe da Silveira

Assigning to has_many :through using the generated *_ids method does not insert in the same order

Reported by Gabe da Silveira | November 17th, 2009 @ 05:52 PM

Suppose you have:

class Post
  has_many :readers
  has_many :people, :through => :readers

The following assignment will insert in the expected order:

@post.people = [@person1, @person2, @person3]

However this assignment will insert in an arbitrary order:

@post.person_ids = [,,]

The fix for this looks a little ugly, but it performs well... we just create a hash of all the fetched records by id, then map the original ids to their respective objects (an O(n) operation in other words).

Attached is failing test case (and confirmation of the original correct behavior for the first case) plus fix. Tested against mysql, postgres and sqlite.

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=""></a>

People watching this ticket