Ro Ma | 23 Jul 06:22

Extracting one column from an array of objects


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@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

giorgio | 23 Jul 06:52

Re: Extracting one column from an array of objects


You should be able to use ids= users.collect {|u| u.id}

G.
On Jul 23, 4:24 pm, Ro Ma <rails-mailing-l...@...> 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@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe <at> googlegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Danny Burkes | 23 Jul 07:34

Re: Extracting one column from an array of objects


> 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@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Samuel Flores | 23 Jul 18:00

Re: Extracting one column from an array of objects


users.map { |user| user.id }

Samuel Flores

Em 23/07/2008, às 01:24, Ro Ma <rails-mailing-list@...>  
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@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe <at> googlegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
(Continue reading)


Gmane