Rails model attr_accessor attribute not saving?

落爺英雄遲暮 提交于 2019-12-06 05:13:16

attr_accessor is a built-in Ruby macro which will define a setter and a getter for an instance variable of an object, and doesn't have anything to do with database columns with ActiveRecord instances. For example:

class Animal
  attr_accessor :legs
end

a = Animal.new
a.legs = 4
a.legs #=> 4

If you want it to be saved to the database, you need to define a column in a migration. Then ActiveRecord will create the accessor methods automatically, and you can (should) remove your attr_accessor declaration.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!