Forum: Ruby on Rails How do I get an array of object ids?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ray B. (Guest)
on 2006-03-09 09:55
(Received via mailing list)
If a = object, then a[:id] => 11 (ie, the primary key value of object.

But if bs = [ object1, object2], then bs.each { |b| = b[:id] } => bs

What I want is an array of the object ids?

Suggestions?
Greg Moreno (Guest)
on 2006-03-09 10:57
Hi Ray,

Try this:  a = bs.collect { |b| b.id }

Ray B. wrote:
> If a = object, then a[:id] => 11 (ie, the primary key value of object.
>
> But if bs = [ object1, object2], then bs.each { |b| = b[:id] } => bs
>
> What I want is an array of the object ids?
>
> Suggestions?
Jonathan V. (Guest)
on 2006-03-09 14:03
I don't quite follow your example, but if you want a method which copes
with either a single object, or an array of objects, this should work:

def object_ids(*objects)
  objects.flatten.collect { |o| o.id }
end

-Jonny.
This topic is locked and can not be replied to.