This feels like it should be easy but i can''t work it out: if i have a collection of stories, ordered by (for example) date, then how do i find out the position of the story with id = (eg) 23 in the collection? I guess this is a ruby question rather than a rails-specific question, but maybe there''s a rails way... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Here is my 2cents worth, but I would love to learn a better way.> > the_collection = User.find(:all) > > the_collection.map(&:id).index(23). > > cheers > ivor > > > On 9/4/07, Max Williams <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > > > > This feels like it should be easy but i can''t work it out: if i have a > > collection of stories, ordered by (for example) date, then how do i find > > out the position of the story with id = (eg) 23 in the collection? > > > > I guess this is a ruby question rather than a rails-specific question, > > but maybe there''s a rails way... > > -- > > Posted via http://www.ruby-forum.com/. > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
There must be a better way than this, but it''ll work ;) You can try iterating trough the collection with a counter, and setting the position value on the hit. Something like this: counter = 0 position = 0 for story in @stories if story.id == 23 position = counter end counter = counter + 1 end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ivan Trajkovic wrote:> There must be a better way than this, but it''ll work ;) > > You can try iterating trough the collection with a counter, and setting > the position value on the hit. Something like this: > > counter = 0 > position = 0 > > for story in @stories > if story.id == 23 > position = counter > end > counter = counter + 1 > endThanks ivan, i ended up doing something similar but a little bit nicer after a suggestion elsewhere: @stories.each_with_index do |item, index| if item.id == 23 position = index break end end thanks for helping though :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---