Gandalf StormCrow
Gandalf StormCrow

Reputation: 26192

Return activerecord record property as a string

If I wanted to get Users' name I'd do something like this

User.select(:name).find(55)

This would return :

#<User name: "Peter">

How do I get just "Peter".

Upvotes: 0

Views: 93

Answers (2)

yannick
yannick

Reputation: 661

If you just want the names and you want to avoid object instanciation

User.where(id: 55).pluck(:name).first

And you will totally avoid the cost of instanciating an activeRecord object

Upvotes: 2

Lu&#237;s Ramalho
Lu&#237;s Ramalho

Reputation: 10198

Just call .name, like so

user = User.select(:name).find(55)
user.name 

Or

User.select(:name).find(55).name

Upvotes: 1

Related Questions