203

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
Backo
  • 18,291
  • 27
  • 103
  • 170
  • 6
    I love the terminology applied '"proper"/"correct"/"sure"'. When you give it 3 ways it really ensures there is no misinterpretation. Good Job! – Jay May 08 '12 at 18:54
  • 5
    @Jay - "Fineness italianisms" ; - ) – Backo May 08 '12 at 18:57
  • 3
    Just to be clear, the "stack level too deep" is referring to the fact that its a recursive call ... its calling itself. – Nippysaurus Jan 29 '14 at 05:48

5 Answers5

325

=========================================================================== Update: July 19, 2017

Now the Rails documentation is also suggesting to use super like this:

class Model < ActiveRecord::Base

  def attribute_name=(value)
    # custom actions
    ###
    super(value)
  end

end

===========================================================================

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.

class Model < ActiveRecord::Base
  attr_accessible :attribute_name

  def attribute_name=(value)
    # custom actions
    ###
    write_attribute(:attribute_name, value)
    # this is same as self[:attribute_name] = value
  end

end

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:

class Person
  attr_accessor :name
end

class NewPerson < Person
  def name=(value)
    # do something
    @name = value
  end
end

This will be easier to understand once you know what attr_accessor does. The code attr_accessor :name is equivalent to these two methods (getter and setter)

def name # getter
  @name
end

def name=(value) #  setter
  @name = value
end

Also your second method fails because it will cause an infinite loop as you are calling the same method attribute_name= inside that method.

rubyprince
  • 17,559
  • 11
  • 64
  • 104
  • 9
    For Rails 4 just skip `attr_accessible` since it's not there anymore, and it should work – zigomir Jan 17 '14 at 21:20
  • 11
    Why not call `super`? – Nathan Lilienthal Jan 22 '14 at 16:43
  • 1
    I was of the impression that since accessors and writers are created dynamically, `super` might not work. But, it seems it is not the case. I just checked it and it works for me. Also, this [question](http://stackoverflow.com/questions/373731/override-activerecord-attribute-methods) ask the same – rubyprince Jan 23 '14 at 10:14
  • 4
    There is a huge gotcha with ```write_attribute```. Conversions will be skipped. Be aware that ```write_attribute``` will skip timezone conversions with dates, which will almost always be undesired. – Tim Scott May 23 '15 at 16:57
  • 2
    super will work as well however there are some reason that you might not want to us it. For example in mongoid gem there is a bug where you can't push to the array if you super the getter method. It is bug because of there way the manage the array in memory. Also the @name will also return the value set rather then call the method that your overwriting. However in the above solution both will work just fine. – User128848244 Jul 29 '15 at 18:32
  • @TimScott Is this still the case? I can't see anything funny [here](https://github.com/rails/rails/blob/89e2f7e/activerecord/lib/active_record/attribute_methods/write.rb#L18-21). Not that I argue for `super` not being enough. – x-yuri Jul 06 '16 at 10:38
  • attr_accessible is replaced with strong parameters after Rails 4 - so just add the virtual attribute to permitted parameters in the controller. – msdundar Feb 22 '17 at 16:26
50

Use the super keyword:

def attribute_name=(value)
  super(value.some_custom_encode)
end

Conversely, to override the reader:

def attribute_name
  super.some_custom_decode
end
Robert Kajic
  • 8,689
  • 4
  • 44
  • 43
  • 1
    Better answer than the accepted IMO since it keeps the method call limited to the same name. This preserves inherited overridden behavior to attribute_name= – Andrew Schwartz Feb 02 '16 at 08:10
  • Overriding the getter method has become hazardous in Rails 4.2 due to this change: https://github.com/rails/rails/commit/787e22bb491bd8c36db1e9734261c4ce02c5c5fd Previously form helpers would call the untypecast value of the field and not call your custom getter. Now they call your method, and so will produce confusing results in your forms depending on how you're overriding the value. – Brendon Muir Dec 05 '16 at 05:57
16

In rails 4

let say you have age attribute in your table

def age=(dob)   
    now = Time.now.utc.to_date
    age = now.year - dob.year - ((now.month > dob.month || (now.month == dob.month && now.day >= dob.day)) ? 0 : 1)
    super(age) #must add this otherwise you need to add this thing and place the value which you want to save. 
  end

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.

Taimoor Changaiz
  • 10,250
  • 4
  • 49
  • 53
3

I have found that (at least for ActiveRecord relationship collections) the following pattern works:

has_many :specialties

def specialty_ids=(values)
  super values.uniq.first(3)
end

(This grabs the first 3 non-duplicate entries in the array passed.)

Robin Daugherty
  • 7,115
  • 4
  • 45
  • 59
0

Using attr_writer to overwrite setter attr_writer :attribute_name

  def attribute_name=(value)
    # manipulate value
    # then send result to the default setter
    super(result)
  end
Weibo Chen
  • 369
  • 1
  • 10