Forum: Ruby on Rails Writing a simple accessor ... or so I thought

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D7cb4a1c32b57046567d5009e366f0cf?d=identicon&s=25 carp __ (carp)
on 2007-02-07 17:06
Hello Rubyists.

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.

What am I missing here? Thanks in advance.

-carp
D7cb4a1c32b57046567d5009e366f0cf?d=identicon&s=25 carp __ (carp)
on 2007-02-07 17:07
> in a subclass of ActiveController::Base.

... must of course be "ActiveRecord::Base"

:-p
8da92d4ed91aa12535f6d870fa76b25e?d=identicon&s=25 Aaron (Guest)
on 2007-02-07 18:45
(Received via mailing list)
> class User < ActiveRecord::Base
>   attr_reader :login
>
>   def login=(new_login)
>     @login = new_login unless !new_record?
>   end
> end
>

Try this:
def login=(new_login)
  write_attribute( :login, new_login ) if new_record?
end

For more info take a look at the "Overwriting default accessors"
section of the ActiveRecord::Base documentation.
http://api.rubyonrails.com/classes/ActiveRecord/Base.html

Aaron
D7cb4a1c32b57046567d5009e366f0cf?d=identicon&s=25 carp __ (carp)
on 2007-02-08 10:29
Aaron wrote:
> For more info take a look at the "Overwriting default accessors"
> section of the ActiveRecord::Base documentation.
> http://api.rubyonrails.com/classes/ActiveRecord/Base.html

I was able to fix it. I've been searching the forever, but never got to
the section you pointed out. Thanks a lot Aaron!

-carp
This topic is locked and can not be replied to.