D-Nice
D-Nice

Reputation: 4870

Select hash id from an array of hashes

Let's say I have an array with multiple hashes

[#<Campaign id: 144> , #<Campaign id: 146>]

I've stripped out the other fields for simplicities sake, but the object does have many fields. What I want to end up with is an array of unique hash IDs, for example: [144, 146]. Of course there are many ways to do this naively, but I want to know what the best way to do it is. I'm struggling to find a function that was built for this purpose.

Upvotes: 2

Views: 3583

Answers (2)

Silvio Relli
Silvio Relli

Reputation: 399

your_array.map(&:id)

or

your_array.map{|i| i.id}

Upvotes: 7

Michael Berkowski
Michael Berkowski

Reputation: 270637

What you have there appears not to be an array of hashes, but rather an array of some other type of object (Campaign). You should be able to get this via the object_id property and .map():

your_array.map(&:object_id)

Upvotes: 4

Related Questions