I'm guessing how to retrieve just the names of all the records from a database. The easy way is something like this:
@user=User.all
@list=[]
@user.each do|u|
@list.push(u.name)
end
but I'm sure if it has to be a oneline query on irb that must fit. Does anyone has an idea?
You should try :
User.all.collect(&:name)
or
User.select("name")
There is a built-in method to select one attribute, pluck
User.pluck :name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With