Jngai1297
Jngai1297

Reputation: 2495

Rails Console Inserting Data into a Record

This is what I did

Customer.all

  Customer Load (0.6ms)  SELECT "customers".* FROM "customers"
 => #<ActiveRecord::Relation [#<Customer id: 1, name: "Judy Ngai", created_at: "2013-08-13 18:50:02", updated_at: "2013-08-13 18:50:02", phone_number: nil>]> 

then

judy = Customer.find(1) 
insertdata = judy.phone_number = "1234567890"
insertdata.save!
or 
insertdata.save

gives me

NoMethodError: undefined method `save' for "6265607524":String
NoMethodError: undefined method `save!' for "6265607524":String

What should I do?

Upvotes: 1

Views: 2915

Answers (2)

mikesterlingw
mikesterlingw

Reputation: 98

I prefer to use .update_attributes like so:

judy = Customer.find(1)
judy.update_attributes(:phone_number, "1234567890")

If you use update_attributes it performs validation as well (which =/save do not do). For a list of different way to assign attributes check out 5 Ways to set Attributes in ActiveRecord

Upvotes: 2

Luiz E.
Luiz E.

Reputation: 7249

judy = Customer.find(1) 
judy.phone_number = "1234567890"
judy.save!

Upvotes: 4

Related Questions