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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Carsten Zimmermann
2007-Feb-07 16:07 UTC
Re: Writing a simple accessor ... or so I thought
> in a subclass of ActiveController::Base.... must of course be "ActiveRecord::Base" :-p -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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.htmlI 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---