Compatibility with ActiveRecord nested attributes #16

Closed
joslynesser opened this Issue Mar 29, 2012 · 13 comments

6 participants

@joslynesser

I'm trying to figure out a good solution for handling nested attribute parameters coming in through the controller and wanted to get some input from you guys. While there is now support for nested parameters, it doesn't currently play nice with form#fields_for and ActiveRecord nested attributes.

As an example, here are the parameters that get submitted for a Parent that has many Children:

{ 
  "parent" => {
    "children_attributes" => {
      "0" => {"id" => "1", "public" => "Public", "private" => "Private" }, 
      "1" => {"id" => "2", "public" => "Public", "private" => "Private" }
    } 
  } 
}

With the current implementation of permit and nested parameters, I need to first convert nested attributes into an array using code similar to what already exists in ActiveRecord: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/nested_attributes.rb#L398

def permitted_params
  params[:parent][:children_attributes] = params[:parent][:children_attributes].values
  params.required(:parent).permit(:children_attributes => [:public])
end

Should this be a feature included in strong_parameters to play nicely with ActiveRecord nested attributes? I'm happy to start working on it, but would love any feedback on whether or not it should be included before diving in :)

Thanks!

  • Joslyn
@ghost

Also trying to figure this out...

@bloudermilk

Just hit the same wall...

/cc @dhh

@bloudermilk

Just found this on the Rails blog:

More work to be done

We're still working on a tidy way to deal with nested parameters, but there's a design ready for implementation, so it shouldn't be far off.

If someone wants to share the mentioned design I'd be happy to implement it.

@bloudermilk

I ended up writing a simple before_filter that circumvents this issue for all params in a Rails app. It's working great in our app.

@erichmenge

@bloudermilk Looks pretty good, have you considered doing a pull request?

@bloudermilk

@erichmenge Unfortunately I don't have the time to commit to that at the moment (no pun intended). This is just something I whipped up for the project I'm currently on. Also it seems like the @rails team has abandoned this gem since @dhh published it some months ago.

@erichmenge

@bloudermilk One of the Rails team started to integrate this into Rails 4 and was told to wait by a Core team member because they haven't figured out exactly how they're going to do it yet.

I don't think they'll end up using a gem, but will pull over this code into Rails master when/if the time comes.

I am a bit concerned they've thought of a new approach and have decided against this method, being I'm using this approach on my latest project.

It would be nice to get an update on if the strong_parameters approach is still in the plan or what.

@erichmenge

@spastorino Would a PR request that implements something like this be welcome?

@spastorino
Ruby on Rails member

/cc @dhh

@dhh
Ruby on Rails member
dhh commented Jun 7, 2012

This is definitely going into Rails core. We've abandoned doing the form signing idea, but what's in this plugin is going into core.

Can someone explain again what the problem is with the nested attributes? Why isn't the nested permit we have right now good enough? (explanation in form of a failing test would be welcome!)

@erichmenge

@dhh Sure, I'll try to get something worked up shortly.

@erichmenge

This issue can be closed now. /cc @rafaelfranca

@rafaelfranca
Ruby on Rails member

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment