I'm looking for an easy/fast way of getting an array of ids from a Active Record relation.
Currently i have:
product_ids = Product.select(:id).where(:colour => 'blue').all.map{|p|p.id}
But that's messy and requires a map..
Something like this would be cooler:
product_ids = Product.where(:colour => 'blue').ids
Any ideas?
Thanks :)
A little bit more neat solution:
Product.where(:colour => 'blue').pluck(:id)
In recent versions of Rails the ids
method can be used.
Product.where(color: 'blue').ids
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With