Skip to content


Subversion checkout URL

You can clone with
Download ZIP
A proof-of-concept for adding nested mass assignment to ActiveRecord.
Fetching latest commit...
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


= NestedAssignment

An attempt at solving the model (and controller) side of nested forms. Based on thoughts pulled from,, and work on ActiveScaffold.

== Goals:

# Assigning attributes should remain in-memory. Nothing should be permanent before #save. This means avoiding some of ActiveRecord's association assignment logic such as AssociationCollection#replace.
# Deep and thorough validation of all new and changed associated records. In order for all possible errors to be available at once, no single invalid record may halt the process.
# Support HTML-only forms. This means using a :_delete param to delete records, since that can be accomplished with a checkbox and does not require JavaScript to remove input fields from the form data. Credit to Josh Susser.
# Use parameter hashes for collections instead of arrays, because params order can't be guaranteed through AJAX (credit to James Golick) and Rails' parameter parser has troubles with arrays of complex objects (e.g. params[:user][:photos][][:title]). The hash key should not be meaningful (such as the record id) or magical (such as new_1234), so that it may be generated by any means. Using a timestamp is particularly effective, since it also provides a maintainable order (credit to Eloy Duran).
# Nested mass assignment should be enabled manually for security and performance reasons.
# Nested mass assignment params should be keyed with a suffix (e.g. _params) to avoid complicating association assignment logic. This means params[:user][:photos_params] instead of params[:user][:photos], which assigns via User#photo_params= instead of User#photos=. The former can be tailored to the specific needs of form input, whereas the latter is best for in-memory association management. This constraint could conceivably be removed later, once mass assignment is more mature.

and of course:

# Skinny controllers. There's no footprint in the controller, actually.

== Paradigm Params (cf

    :task_params =>
        '1' => { :id => '3', :name => "Foo" },
        '2' => { :id => '5', :name => "Bar", :_delete => true}
        '3' => { :name => "Baz" }

Copyright (c) 2008 Lance Ivy, released under the MIT license
Something went wrong with that request. Please try again.