I got a model relationships
Country <- (hm) city <- (hm) Address
The table city has two fields (both required)
integer :country_id
string :name
When creating a city the user can type a country in a country field
<%form_for @city do |f| %>
<%= f.label :country %>
<%= f.text_field :country %>
<%= f.label :country %>
<%= f.text_field :country %>
<%end%>
Before creating i would like to find a country by its name, if the
name wouldn't exist then a new country would be created and it's `id`
(the new created ones) would be placed in country_id (so
self.country_id = id) field of the new city.
Then if it validated a new city would be saved to database.
I was trying to do this in the model but couldn't succeed.
My general idea was to do it with a callback like before_create, but
it failed.
I got a model relationships
Country <- (hm) city <- (hm) Address
The table city has two fields (both required)
integer :country_id
string :name
When creating a city the user can type a country in a country field
<%form_for @city do |f| %>
<%= f.label :country %>
<%= f.text_field :country %>
<%= f.label :country %>
<%= f.text_field :country %>
<%end%>
Before creating i would like to find a country by its name, if the
name wouldn't exist then a new country would be created and it's `id`
(the new created ones) would be placed in country_id (so
self.country_id = id) field of the new city.
Then if it validated a new city would be saved to database.
I was trying to do this in the model but couldn't succeed.
My general idea was to do it with a callback like before_create, but
it failed.