Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is attr_accessor necessary in Rails?

I occasionally see attribute accessors/readers/writers in the code for models. Are these necessary if I want to be able to update attributes from the view / controller code?

I am a beginner so I am really talking about basic applications.

like image 276
max pleaner Avatar asked Dec 12 '13 02:12

max pleaner


People also ask

What is the use of Attr_accessor in rails?

attr_accessor is used to define an attribute for object of Model which is not mapped with any column in database.

What is the use of Attr_accessor in Ruby?

In Ruby, object methods are public by default, while data is private. To access and modify data, we use the attr_reader and attr_writer . attr_accessor is a shortcut method when you need both attr_reader and attr_writer .

What does Attr_reader do in Ruby?

attr_reader and attr_writer in Ruby allow us to access and modify instance variables using the . notation by creating getter and setter methods automatically. These methods allow us to access instance variables from outside the scope of the class definition.

What is attribute in Ruby?

Attributes are specific properties of an object. Methods are capabilities of an object. In Ruby all instance variables (attributes) are private by default. It means you don't have access to them outside the scope of the instance itself. The only way to access the attribute is using an accessor method.


3 Answers

attr_accessor is a core feature of Ruby and is used to generate instance variables with getter and setter methods. Its use is never required in basic Ruby (it's a convenience).

In the case of ActiveRecord models, getters and setters are already generated by ActiveRecord for your data columns. attr_accessor is not needed or desirable.

If you have additional instance data you don't need to persist (i.e. it's not a database column), you could then use attr_accessor to save yourself a few lines of code.

The similarly-named attr_accessible — which is frequently seen in Rails code and confused with attr_accessor — is a deprecated method of controlling mass assignment within ActiveRecord models. Rails 4 doesn't support it out of the box; it has been replaced by Strong Parameters, which allows more granular control.

like image 163
colinm Avatar answered Oct 08 '22 10:10

colinm


If you declare an attr_accessor then you can use it as a virtual attribute, which is basically an attribute on the model that isn't persisted to the database.

Example case: you declare attr_accessor :password in your User model so that you can use it as a field in a new user form. When you receive their password in the corresponding create action, you can derive a hashed_password, persist it to the database, and discard the given password (which is done automatically at the end of the request).

like image 28
Kaleidoscope Avatar answered Oct 08 '22 10:10

Kaleidoscope


Generally it is a pretty good idea to decorate attr_accessor for anything on a model that is not an actual column in the SQL table. Rails 4 or not. This gives you clear understanding of what's in the model and what is persisted.

like image 3
Nikolay Avatar answered Oct 08 '22 09:10

Nikolay