Backo
Backo

Reputation: 18871

'attr_accessible' effects

I am using Ruby on Rails 3.0.9 and I would like to know in which cases (that is, for which methods) the attr_accessible method has effect. For example, if I use

attr_accessible :name, :surname

it will care to not assign those attribute values when you use the new(...) method for the User.new(params[:user]) statement.

But what other methods it will take care? Can I run correctly, for example, methods as where(...) and exists?(...) without that the attr_accessible will take effect?

Upvotes: 1

Views: 565

Answers (2)

rubyprince
rubyprince

Reputation: 17793

If you use attr_accessible, the model will prevent mass assignment of those columns which are not included in the attr_accessible list. The methods affected are those of mass assignment like new, create, update_attributes, attributes= etc. All other functions will work, even single assignment like this:

@model_object.column_not_listed_in_attr_accessible_list = "Saved"
@model_object.column_not_listed_in_attr_accessible_list
=> "Saved"

So, there should not be any problem for using them in where, exists? etc.

Upvotes: 3

Arun Kumar Arjunan
Arun Kumar Arjunan

Reputation: 6857

attr_accessible will impact only functions that is related to write operations.

Ex: new, create, update_attributes, etc.

Other read-only functions like where, exists?, etc should not have any impact.

Upvotes: 2

Related Questions