[LRUG] Query about ActiveRecord

Tom Stuart tom at experthuman.com
Fri Aug 17 11:23:45 PDT 2007


On 17 Aug 2007, at 18:58, John Winters wrote:
>    def pattern=(new_pattern)
>      @pattern = new_pattern
>    end
>
> This ran OK, but the new value was not saved to the database.

Do "self[:pattern] = new_pattern", or the equivalent "write_attribute 
(:pattern, new_pattern)".

See the "Overwriting default accessors" section of http:// 
api.rubyonrails.org/classes/ActiveRecord/Base.html for more details.

(In general you can use alias_method_chain to work around these "I  
want to wrap behaviour around an existing method without changing its  
name" problems -- see http://weblog.rubyonrails.org/2006/4/26/new-in- 
rails-module-alias_method_chain -- but that's overkill in this case.)

Cheers,
-Tom




More information about the Chat mailing list