What is the right way to override a setter method in Ruby on Rails?

I am using Ruby on Rails 3.2.2 and I would like to know if the following is a “proper”https://stackoverflow.com/”correct”https://stackoverflow.com/”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”https://stackoverflow.com/”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

5 Answers
5

Leave a Comment