Read more

Shortcut for getting ids for an ActiveRecord scope

Martin Schaflitzl
March 21, 2019Software engineer at makandra GmbH

You can use .ids on an ActiveRecord 4+ scope to pluck all the ids of the relation

User.where("users.name LIKE 'Foo Bar'").ids

# same as (since Rails 3.2)
User.where("users.name LIKE 'Foo Bar'").pluck(:id)
Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

If you are stuck on Rails 2.3 you can use Edge Rider's collect_ids Show archive.org snapshot :

User.where("users.name LIKE 'Foo Bar'").collect_ids(:id)
Posted by Martin Schaflitzl to makandra dev (2019-03-21 11:33)