Sort of a newbie question - sorry if it''s been asked before and I missed it in my search. I have an id_array of Author ids, and I want to retrieve the associated Author models as quickly as possible, but also preserve the ordering of the id_array. Using Author.find(id_array) returns the records, but not in the same order. Is there some flag I can pass to find() to preserve the order? I want to avoid looping through the id array and finding the models individually. Thanks in advance, John -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060715/0d2c87e4/attachment.html
Josh Susser
2006-Jul-15 23:46 UTC
[Rails] Re: find() with id array - can i preserve the ordering?
john li wrote:> I have an id_array of Author ids, and I want to retrieve the associated > Author models as quickly as possible, but also preserve the ordering of > the id_array.Author.find(id_array).sort_by { |e| id_array.index(e.id) } -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/.
john li
2006-Jul-16 00:17 UTC
[Rails] Re: find() with id array - can i preserve the ordering?
Ah, I was wondering if there was some simple built-in flag to do it, but that code snippet should definitely work too. Thanks! On 7/15/06, Josh Susser <josh@hasmanythrough.com> wrote:> > john li wrote: > > I have an id_array of Author ids, and I want to retrieve the associated > > Author models as quickly as possible, but also preserve the ordering of > > the id_array. > > Author.find(id_array).sort_by { |e| id_array.index(e.id) } > > -- > Josh Susser > http://blog.hasmanythrough.com > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- John Li Two-Bit Operation LLC http://www.twobitoperation.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060716/b3578637/attachment.html