Module: MassAssignment
- Included in:
- ActiveRecord::Base
- Defined in:
- lib/mass_assignment.rb
Overview
Implements the mass_assign instance and class methods. Include in your class for easy mass assignments.
class Book
attr_accessor :title, :author
include MassAssignment
end
book = Book.mass_assign(:title => 'The Little Red Book', :author => 'Jenny from the Block')
book.title #=> 'The Little Red Book'
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#mass_assign(attributes) ⇒ Object
Sends all the attributes in the attribute hash to the appropriate attribute writer.
Class Method Details
.included(klass) ⇒ Object
11 12 13 |
# File 'lib/mass_assignment.rb', line 11 def self.included(klass) klass.extend ClassMethods end |
Instance Method Details
#mass_assign(attributes) ⇒ Object
Sends all the attributes in the attribute hash to the appropriate attribute writer. This is a way to circumvent the normal attribute protection on Rails models.
Example:
user.mass_assign(:username => 'Manfred', :password => 'very secret')
Instead of:
user.username = 'Manfred'
user.password = 'very secret'
45 46 47 |
# File 'lib/mass_assignment.rb', line 45 def mass_assign(attributes) assign_attributes(attributes, :without_protection => true) end |