This seems really simple but I can''t seem to find an answer. Say I have an array of Users which has a column called ''id''. I need an array that holds just the ids. Right now I''m doing: for user in users ids << user.id end Is there a method to do this? Note that these objects aren''t from my database so I can''t use find. -- 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 -~----------~----~----~----~------~----~------~--~---
You should be able to use ids= users.collect {|u| u.id}
G.
On Jul 23, 4:24 pm, Ro Ma
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> This seems really simple but I can''t seem to find an answer.
>
> Say I have an array of Users which has a column called
''id''. I need an
> array that holds just the ids. Right now I''m doing:
>
> for user in users
> ids << user.id
> end
>
> Is there a method to do this? Note that these objects aren''t from
my
> database so I can''t use find.
> --
> Posted viahttp://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@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
> Say I have an array of Users which has a column called ''id''. I need an > array that holds just the ids.ids = users.collect(&:id) -- 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 -~----------~----~----~----~------~----~------~--~---
users.map { |user| user.id }
Samuel Flores
Em 23/07/2008, às 01:24, Ro Ma
<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
escreveu:
!#x000a>
> This seems really simple but I can''t seem to find an answer.
>
> Say I have an array of Users which has a column called
''id''. I need an
> array that holds just the ids. Right now I''m doing:
>
> for user in users
> ids << user.id
> end
>
> Is there a method to do this? Note that these objects aren''t from
my
> database so I can''t use find.
> --
> 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@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---