开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜