I don’t know about you, but this post made me pretty happy. Until the related changeset got pulled for very good reasons. Well, my current project needs nested mass assignment, and none of the workarounds made me happy. So after reading the discussion and trying to understand the various problems and current thinking, I set out to create a plugin.
Behold, the next in my series of cleverly named plugins on Github: nested_assignment.
It requires you to specify which associations are mass assignable:
class User < ActiveRecord::Base has_one :avatar has_many :tasks accessible_associations :avatar, :tasks end
And then works from a parameter structure like:
{
# singular associations
:avatar_params => {
:id => '7',
:name => 'mugshot.jpg'
},
# plural associations
:tasks_params =>
{
# create
'1' => { :name => "Baz" },
# update
'2' => { :id => '3', :name => "Foo" },
# destroy
'3' => { :id => '5', :name => "Bar", :_delete => '1'}
}
}WARNING
I just finished writing test coverage for this plugin in a very controlled environment, so that I could make it available as a proof of concept. That means this is most certainly use at your own risk.