Posted over 6 years ago. Visible to the public. Repeats. Linked content.

Rails: Overwriting default accessors

All columns of a model's database table are automagically available through accessors on the Active Record object.

When you need to specialize this behavior, you may overwrite the default accessors (using the same name as the attribute) and call read_attribute(attr_name) and write_attribute(attr_name, value) to actually change things. Example:

class Poet < ActiveRecord::Base def name=(name) write_attribute(:name, name.strip) end end

See also

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Owner of this card:

Dominik Schöler
Last edit:
over 2 years ago
by Henning Koch
getter, setter
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Dominik Schöler to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more