# Select an existing item or create a new one

**URL:** <https://www.ruby-forum.com/t/select-an-existing-item-or-create-a-new-one/186481>\
**Category:** Rails\
**Created:** [April 7, 2010, 6:21pm UTC](https://www.ruby-forum.com/t/select-an-existing-item-or-create-a-new-one/186481 "2010-04-07T18:21:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![keavy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ea666f/32.png) [@keavy](https://www.ruby-forum.com/u/keavy)\
**Post date:** [April 7, 2010, 6:21pm UTC](https://www.ruby-forum.com/t/select-an-existing-item-or-create-a-new-one/186481/1 "2010-04-07T18:21:47Z")

</div>

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:

# option 1: remove the location\_attributes at the controller level, if

an existing location\_id is passed

If so, this is the snippet of code I’m using in a before\_filter on the  
create action:  
params[:event][:happenings\_attributes].each do |h|  
unless h[1][“location\_id”].blank?  
h[1].delete(“location\_attributes”)  
end  
end

- which goes ignored and the validations for creating a new location  
are triggered.

I got to the h[1] level by playing around in console like so:  
params = {  
“event”=\>{  
“name”=\>"",  
“happenings\_attributes”=\>{  
“0”=\>{  
“start\_at” =\> ‘2010-03-02’,  
“location\_attributes”=\>{  
“city”=\>"",  
“country”=\>"",  
“state”=\>""  
},  
“location\_id”=\>“26”}  
}  
}  
}

> > params[‘event’][‘happenings\_attributes’].each do |h|  
> > ?\> puts h.class
> 
> > end  
> > Array  
> > =\> {“0”=\>{“location\_attributes”=\>{“city”=\>"", “country”=\>"",  
> > “state”=\>""}, “start\_at”=\>“2010-03-02”, “location\_id”=\>“26”}}
> 
> > params[‘event’][‘happenings\_attributes’].each do |h|  
> > ?\> puts h[1][“location\_id”]
> 
> > puts h[1][“location\_attributes”]  
> > end  
> > 26  
> > citycountrystate

* * *

# option 2: remove the location\_attributes at the model level

# Happening model

before\_validation :check\_new\_or\_existing\_location

def check\_new\_or\_existing\_location  
if !location\_id.blank?  
location\_attributes= nil  
end  
end

* * *

Any suggestions appreciated 🙂

---

<div class="post-metadata">

**Author:** ![keavy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ea666f/32.png) [@keavy](https://www.ruby-forum.com/u/keavy)\
**Post date:** [April 7, 2010, 9:56pm UTC](https://www.ruby-forum.com/t/select-an-existing-item-or-create-a-new-one/186481/2 "2010-04-07T21:56:39Z")

</div>

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

> **[ActiveRecord::Base](https://api.rubyonrails.org/classes/ActiveRecord/Base.html)**
>
> Active Record Active Record objects don't specify their attributes directly, but rather infer them from the table definition with which they're linked.

---

<div class="post-metadata">

**Author:** ![keavy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ea666f/32.png) [@keavy](https://www.ruby-forum.com/u/keavy)\
**Post date:** [April 7, 2010, 10:28pm UTC](https://www.ruby-forum.com/t/select-an-existing-item-or-create-a-new-one/186481/3 "2010-04-07T22:28:50Z")

</div>

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.
