Shortcut for getting ids for an ActiveRecord scope

Updated . Posted . Visible to the public. Repeats.

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

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

# same as (since Rails 3.2)
User.where(" LIKE 'Foo Bar'").pluck(:id)

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

User.where(" LIKE 'Foo Bar'").collect_ids(:id)
Martin Schaflitzl
Last edit
Deleted user #4117
Source code in this card is licensed under the MIT License.
Posted by Martin Schaflitzl to makandra dev (2019-03-21 10:33)