What's the use case for this? The objects aren't going to come out of
the DB in order the next time you load the object unless you're
putting them in order with options on the association that you haven't
shown here.
--Matt Jones
>
> :order => 'id ASC' on the association that the has_many :through is
> going through.
>
> In MySQL incidentally will usually give you this by default.
"Usually" being the key word. In your case, it isn't doing that, as
all that the find() call is doing is generating a query like:
SELECT * FROM table_name WHERE id IN (x,y,z,etc)
If those records aren't coming back in the order you expect, that's
not Rails's fault...
--Matt Jones
>
> Matt, your comment doesn't make any sense. I'm not sure what you are
> thinking, but you are clearly not understanding the issue.
Whoops - looks like I was reading the question the other way around
(that you *wanted* the records to come back in order). Sorry about that!
> This is
> not my own method, it is a method generated by the has_many :through
> macro. Consider this concrete example:
>
> Person has_many :routes
> Person has_many :destinations, :through => :routes
>
> Now say I do, @person.desination_ids = [5, 8, 3]
>
> That is what the included method definition does. It's totally
> reasonable to expect the first route insertion to be for destination
> 5, the second insertion to be for destination 8 and the third
> insertion to be for desination 3. However it doesn't do this as you
> pointed out due to the fact that it finds all the destinations without
> any order. However as I tried to point out in the original post,
> there is NO WAY to maintain the order in the SQL because it's
> arbitrary, it's not sorted on any field.
>
> That is why I redefined the method so that it uses the original array
> to inform the final order of objects that get passed to the
> association method (ie. @person.destinations). As you can see, the
> ordering of my association is neither here nor there. It's the order
> of insertion that matters, and it's not controlled by an order clause.
>
> My question was not how do I solve this, my question was whether
> people think that this should be submitted as a Rails patch. Having
> thought about it further I'm convinced it should be.
For this to make sense as a Rails patch, there's got to be a reason
why this ordering should be preserved. What's the difference between
adding the records in the [5,8,3] order vs. some other order?
--Matt JOnes
It seem to me that ordering on id isn't what you really want here.
What happens when you add the function to reorder the routes? If it
were me I'd probably do something like adding a position attribute to
route and use something like acts_as_list
--
Rick DeNatale
Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale