开发者

Why won't AR allow me to make an attribute setter private using `private :accepted=`?

开发者 https://www.devze.com 2023-03-24 07:03 出处:网络
I\'ve just setup a new boolean accepted attribute on my model that looks like this: class Invitation < ActiveRecord::Base

I've just setup a new boolean accepted attribute on my model that looks like this:

class Invitation < ActiveRecord::Base

  attr_protected :accepted
  ...

end

I want it to be a private attribute however when I attempt开发者_开发问答 to remove the public setter like this:

class Invitation < ActiveRecord::Base

  attr_protected :accepted
  private :accepted=
  ...

end

I get an immediate failure of the type:

invitation.rb:17:in `private': undefined method `accepted=' for class `Invitation' (NameError)

Why isn't AR detecting the setter? I do know that I could do this by defining the method in longhand but I am interested as to why I can't use the shorthand private :accepted= route.


The attribute getters and setters are not actual methods, they are implemented using method_missing in ActiveRecord. That's why you can't manipulate them using private.

0

精彩评论

暂无评论...
验证码 换一张
取 消