What I want is a “write once, then readonly” attribute in a subclass of
ActiveController::Base. With “write once” in this case I mean that it
must not be changed once the object has been stored to the database.
The simplified class looks as follows:
class User < ActiveRecord::Base
attr_reader :login
def login=(new_login) @login = new_login unless !new_record?
end
end
When I tested the method, I noticed that it behaved oddly:
An instance of User is created from the fixtures. Its login attribute is
set properly (checked it with Object::inspect). However, thatobj.login
returns nil. I thought it should be covered with the “attr_reader
:login” statement in the User class.