Skip to content
This repository has been archived by the owner on Aug 17, 2017. It is now read-only.
This repository has been archived by the owner on Aug 17, 2017. It is now read-only.

Setting a nested resource to nil will drop the key #230

Open
@csexton

Description

I want to be able to delete a nested resource when I get a nested attribute set to nil.

Currently have an API that lets you manage nested resources. The model update logic expects the client to set an explicit nil when it wants to remove one of these related resources.

For example I have an API that exposes the People in the system. Those people have an optional Address.

To delete the address from a person set the address parameter to nil.

Everything works fine with the nested address attributes set:

>> params = ActionController::Parameters.new({people: [{ name: "Amy Pond", address: { town: "Leadworth", street: "Cathedral Rd" } }] })
=> {
     "people"=>[{ 
       "name"=>"Amy Pond",
       "address"=>{
         "town"=>"Leadworth",
         "street"=>"Cathedral Rd"
       }
     }]
   }

>> params.permit(:people, :people => [:name, :address => [:town, :street]]) 
=> {
     "people"=>[{
       "name"=>"Amy Pond",
       "address"=> {
         "town"=>"Leadworth",
         "street"=>"Cathedral Rd"
       }
     }]
   }

But if the nested attributes is set to nil, it gets dropped:

>> params2 = ActionController::Parameters.new({people: [{ name: "Amy Pond", address: nil }] })
=> {
     "people"=>[{
       "name"=>"Amy Pond",
       "address"=>nil
     }]
   }
>> params2.permit(:people,
    :people => [:name, :address => [:town, :street]])

=> {"people"=>[{"name"=>"Amy Pond"}]}
=> {
     "people" => [{
       "name"=>"Amy Pond"
     }]
   }

This feels like it might be inconsistent because when I permit the scalar address on that same params hash, it will retain the nil key:

>> params2.permit(:people,
     :people => [:name, :address])

=> {
     "people" => [{
       "name"=>"Amy Pond",
       "address"=> nil
     }]
   }

Is this the desired behavior, or would it be preferred to retain the nil value?

I havn't looked into implemntation, but would be happy to work in a PR if this is a desired behavior.

Activity

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

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions