Using attr_accessor and attr_accessible on the same field

前端 未结 4 1828
误落风尘
误落风尘 2020-12-01 06:42

What happens in the background with the following code?

class User < ActiveRecord::Base

 attr_accessor :name
 attr_accessible :name

end
<
4条回答
  •  庸人自扰
    2020-12-01 07:24

    Since it inherits ActiveRecord, it will be persisted when you call the save method (but not when it is instantiated).

    If you don't have any attributes for that model, I assume ActiveRecord will simply save a new row in the database (i.e. your object will only have a persisted id). This makes sense, as you might later add attributes to your User model, and the persisted instances should still be retrievable.

提交回复
热议问题