Shortcuts for getting ids for an ActiveRecord scope

Updated . Posted . Visible to the public.

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

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

# Rails 3.2+ equivalent
User.where("users.name LIKE 'Foo Bar'").pluck(:id)

# Edge rider equivalent for Rails 2+
User.where("users.name LIKE 'Foo Bar'").collect_ids
Profile picture of Martin Schaflitzl
Martin Schaflitzl
Last edit
Henning Koch
License
Source code in this card is licensed under the MIT License.
Posted by Martin Schaflitzl to makandra dev (2019-03-21 10:33)