I am using Ruby on Rails 3.2.2 and I would like to know if the following is a "proper"/"correct"/"sure" way to override a setter method for a my class attribute.
attr_accessible :attribute_name
def attribute_name=(value)
... # Some custom operation.
self[:attribute_name] = value
end
The above code seems to work as expected. However, I would like to know if, by using the above code, in future I will have problems or, at least, what problems "should I expect"/"could happen" with Ruby on Rails. If that isn't the right way to override a setter method, what is the right way?
Note: If I use the code
attr_accessible :attribute_name
def attribute_name=(value)
... # Some custom operation.
self.attribute_name = value
end
I get the following error:
SystemStackError (stack level too deep):
actionpack (3.2.2) lib/action_dispatch/middleware/reloader.rb:70
Using
attr_writer
to overwrite setter attr_writer :attribute_nameIn rails 4
let say you have age attribute in your table
Note: For new comers in rails 4 you don't need to specify attr_accessible in model. Instead you have to white-list your attributes at controller level using permit method.
I have found that (at least for ActiveRecord relationship collections) the following pattern works:
(This grabs the first 3 non-duplicate entries in the array passed.)
=========================================================================== Update: July 19, 2017
Now the Rails documentation is also suggesting to use
super
like this:===========================================================================
Original Answer
If you want to override the setter methods for columns of a table while accessing through models, this is the way to do it.
See Overriding default accessors in the Rails documentation.
So, your first method is the correct way to override column setters in Models of Ruby on Rails. These accessors are already provided by Rails to access the columns of the table as attributes of the model. This is what we call ActiveRecord ORM mapping.
Also keep in mind that the
attr_accessible
at the top of the model has nothing to do with accessors. It has a completely different functionlity (see this question)But in pure Ruby, if you have defined accessors for a class and want to override the setter, you have to make use of instance variable like this:
This will be easier to understand once you know what
attr_accessor
does. The codeattr_accessor :name
is equivalent to these two methods (getter and setter)Also your second method fails because it will cause an infinite loop as you are calling the same method
attribute_name=
inside that method.Use the
super
keyword:Conversely, to override the reader: