usage of attr_accessor in Rails

Ruby on-RailsRuby

Ruby on-Rails Problem Overview


When do you use attr_reader/attr_writer/attr_accessor in Rails models?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Never, unless you have specific need for it. Automatic database-backed accessors are created for you, so you don't need to worry.

Any attr_accessors you do create will change the relevant @attr on the rails object, but this will be lost when the object is destroyed, unless you stick it back in the database. Sometimes you do want this behavior, but it's unusual in a rails app.

Now in ruby, it's a different story, and you end up using these very frequently. But I'd be surprised if you need them in rails---especially initially.

Solution 2 - Ruby on-Rails

attr_accessor can be used for values you don't want to store in the database directly and that will only exist for the life of the object (e.g. passwords).

attr_reader can be used as one of several alternatives to doing something like this:

def instance_value
  "my value"
end

Solution 3 - Ruby on-Rails

Rails models are just ruby classes that inherit from ActiveRecord::Base. ActiveRecord employs attr_accessors to define getters and setters for the column names that refer to the ruby class's table. It's important to note that this is just for persistence; the models are still just ruby classes.

attr_accessor :foo is simply a shortcut for the following:

def foo=(var)
  @foo = var
end

def foo
  @foo
end

attr_reader :foo is simply a shortcut for the following:

def foo
  @foo
end

attr_writer :foo is a shortcut for the following:

def foo=(var)
  @foo = var
end

attr_accessor is a shortcut for the getter and setter while attr_reader is the shortcut for the getter and attr_writer is a shortcut for just the setter.

In rails, ActiveRecord uses these getters and setters in a convenient way to read and write values to the database. BUT, the database is just the persistence layer. You should be free to use attr_accessor and attr_reader as you would any other ruby class to properly compose your business logic. As you need to get and set attributes of your objects outside of what you need to persist to the database, use the attr_s accordingly.

More info:

http://apidock.com/ruby/Module/attr_accessor

http://www.rubyist.net/~slagell/ruby/accessors.html

https://stackoverflow.com/questions/4370960/what-is-attr-accessor-in-ruby

Solution 4 - Ruby on-Rails

If you are using it to validate the acceptance of the terms_of_service, you should really consider using validates :terms_of_service, :acceptance => true. It will create a virtual attribute and is much more concise.

http://guides.rubyonrails.org/active_record_validations.html#acceptance.

Solution 5 - Ruby on-Rails

One example is to have a number of options stored in one serialized column. Form builder would complain if you try to have a text field for one of these options. You can use attr_accessor to fake it, and then in the update action save it in the serialized column.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEthanView Question on Stackoverflow
Solution 1 - Ruby on-RailsPeterView Answer on Stackoverflow
Solution 2 - Ruby on-RailsNoah ThorpView Answer on Stackoverflow
Solution 3 - Ruby on-RailsbrycemcdView Answer on Stackoverflow
Solution 4 - Ruby on-RailsBenson ClarkView Answer on Stackoverflow
Solution 5 - Ruby on-RailslulalalaView Answer on Stackoverflow