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
Written by Sebastián González
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#