Search code examples
ruby-on-railsfindmodelattribute

Finding a particular attribute's value from a model in Rails


I'm trying to find the value of a particular model's attribute in rails. Here's what my code in the User controller's 'create' method looks like:

@user = User.find(1, :select => :money)
existing_money = @user
puts "#{existing_money}"

In my Heroku logs I'll see a variant of the following output instead of the :money integer for that particular user (with :id 1)

#<User:0x00000004e7cbc0>

Any thoughts? Thanks!


Solution

  • As existing_money is just the object you are seeing the object's ID.
    As you want the money attribute you have to reference that too.

    puts "#{existing_money.money}"