Retrieving the IDs for a Model
Prefer the ids
method to map(&:id)
when you want to retrieve an array of model IDs.
Example
receipt.receive_orders.ids
receipt.receive_orders.map(&:id)
In the example above, using ids
avoids fetching the receive orders from the database.
Rails Source
def ids
pluck primary_key
end
Written on August 25, 2016 by alistairmckinnell