Is there a difference between = and assign_attributes in Rails 3?

Suppose you are in your user controller and you want to change the @user name based on some options available to you.

I want to know if there is a difference between the following:

@user.name = params[:user][:name]

or

@user.assign_attributes({:name=> params[:user][:name]})

Thanks in advance!

+5
source share
2 answers

A great way to figure out issues like diving into a source. I found the method in activerecord/lib/active_record/attribute_assignment.rb Check here.

The method assign_attributeswill actually simply iterate over the specified parameters and sends a message to :name=your model. However, since you may be assigning many attributes, it takes precautionary measures into account when assigning a mass. (i.e. make sure the attribute is specified as attr_accessible).

+7

= (, @user.name = params[:user][:name]) . assign_attributes .

Rails API assign_attributes:

, ( ) , : as.

assign_attributes

+3

All Articles