xmnucg
Last Updated: February 25, 2016
·
357
· sebastialonso

Getting a single attribute from an array of associations in Rails

There's a one liner for this, I can't believe how simple it is, and how much time passed before I realized I could do this.

users = User.limit(10)

You want to grab the :name or :some_other attribute

users.pluck(:name)
# => ['Alice', 'Bob',...., 'Eve']

Never stop being a noob