select an existing item or create a new one

I have a form that manages a few models, using accepts_nested_attributes_for

The new event form passes params like so: "event"=>{    "name"=>"",    "happenings_attributes"=>{      "0"=>{        "location_attributes"=>{          "city"=>"",          "country"=>"",          "state"=>""         },         "location_id"=>"26"}     } }

It all works fine, except I'd like to add a choice of selecting an existing location, or entering values for a new one to be created.

I might be going brain dead - I imagine this is a common thing to do - like selecting an existing address or creating new one on an e- commerce site, but alas I've never done it and can't seem to find relevant search results.

Things I've tried:

It sounds like you are looking for something like ActiveRecord's find_or_create_by...() or find_or_initialize_by...() methods.

You might look into "Dynamic attribute-based finders" on

Thanks for your reply. Yeah I didn't think that was suitable in this case, with several fields, showing the user both options in the form etc.

Anyway, I just figured out the issue...

I was using resource_controller and was trying to delete the nested attributes (if not required) in a create.before... method. If I change that to using a regular create method it works fine.