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
.
精彩评论