I always forget the command for doing this so I’m posting it here so I can find it easily.
If you want to overwrite accessors in rails (ActiveRecord), you will run into problems if you try to do it the same way you would with a regular Ruby object. For example, say you want to apply a transformation to an attribute as you assign it:
@person.name = 'ralph'
You always want name to be capitalized, so you would like to modify the name= method. With plain Ruby you would just do:
@name = value.capitalize
You can’t do this with ActiveRecord, you will need to use a method named
write_attribute so the method will look like:
There is also a
read_attribute method to help with overwriting the read method on your attributes in ActiveRecord