Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is mass-assignment in Rails 3

Tags:

I have heard couple of people complaining and posting questions about mass-assignment in Rails. I have got same error couple of times and all I did was attr_accessible. But what exactly is mass assignment? could somebody explain with example?

like image 681
Bhushan Lodha Avatar asked Aug 03 '12 18:08

Bhushan Lodha


People also ask

What is mass assignment in Rails?

Mass assignment is a feature of Rails which allows an application to create a record from the values of a hash. Example: User.new(params[:user]) Unfortunately, if there is a user field called admin which controls administrator access, now any user can make themselves an administrator with a query like.

What is mass assignment in ruby?

This is mass assignment: a pattern that is extremely common and implemented in a ton of Ruby libraries, including ActiveRecord. It allows us to initialize objects with a hash, where the key corresponds with the attribute we are setting and the values represent the attribute values.


1 Answers

Mass Assignment is the name Rails gives to the act of constructing your object with a parameters hash. It is "mass assignment" in that you are assigning multiple values to attributes via a single assignment operator.

The following snippets perform mass assignment of the name and topic attribute of the Post model:

Post.new(:name => "John", :topic => "Something") Post.create(:name => "John", :topic => "Something") Post.update_attributes(:name => "John", :topic => "Something") 

In order for this to work, your model must allow mass assignments for each attribute in the hash you're passing in.

There are two situations in which this will fail:

  • You have an attr_accessible declaration which does not include :name
  • You have an attr_protected which does include :name

It recently became the default that attributes had to be manually white-listed via a attr_accessible in order for mass assignment to succeed. Prior to this, the default was for attributes to be assignable unless they were explicitly black-listed attr_protected or any other attribute was white-listed with attr_acessible.

It is important to consider which attributes can be mass assigned because code like this is so common:

@post = Post.new(params[:post]) 

Typically this is used when the user submits a form rendered by a form_for @post. In an ideal world, the params[:post] hash should only contain the fields we displayed on the form. However, it is trivial easy for the user to pass additional fields in their request, so in effect you're allowing a user to set any fields on @post, not just the ones displayed on the form.

Failure to safely use mass assignment has led to several high profile bugs in some pretty big Rails applications, like the one that allowed somebody to inject their own public key into the list of trusted keys on a Github repository and push code directly to a repository they should not have had access to.

like image 188
meagar Avatar answered Sep 25 '22 12:09

meagar