这就是我做的事情
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>]>
然后
judy = Customer.find(1)
insertdata = judy.phone_number = "1234567890"
insertdata.save!
or
insertdata.save
给了我
NoMethodError: undefined method `save' for "6265607524":String
NoMethodError: undefined method `save!' for "6265607524":String
我该怎么办?
答案 0 :(得分:4)
judy = Customer.find(1)
judy.phone_number = "1234567890"
judy.save!
答案 1 :(得分:2)
我更喜欢像这样使用.update_attribute:
judy = Customer.find(1)
judy.update_attributes(:phone_number, "1234567890")
如果您使用update_attributes,它也会执行验证(=
/ save
不执行验证)。有关分配属性的不同方式列表,请查看5 Ways to set Attributes in ActiveRecord