A model inside another model creation

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.

Any ideas on this one?

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.

Any ideas on this one?

Did you try before_validation_on_create?

I've tried that one and i didn't work.

i get a 'undefined method `new_record?' for "Palestinian Territory,
Occupied":String'

the app trace

vendor/rails/activerecord/lib/active_record/associations.rb:903:in
`belongs_to_before_save_for_country'
vendor/rails/activesupport/lib/active_support/callbacks.rb:173:in
`send'
vendor/rails/activesupport/lib/active_support/callbacks.rb:173:in
`evaluate_method'
vendor/rails/activesupport/lib/active_support/callbacks.rb:161:in
`call'
vendor/rails/activesupport/lib/active_support/callbacks.rb:93:in `run'
vendor/rails/activesupport/lib/active_support/callbacks.rb:92:in
`each'
vendor/rails/activesupport/lib/active_support/callbacks.rb:92:in
`send'
vendor/rails/activesupport/lib/active_support/callbacks.rb:92:in `run'
vendor/rails/activesupport/lib/active_support/callbacks.rb:272:in
`run_callbacks'
vendor/rails/activerecord/lib/active_record/callbacks.rb:298:in
`callback'
vendor/rails/activerecord/lib/active_record/callbacks.rb:206:in
`create_or_update'
vendor/rails/activerecord/lib/active_record/base.rb:2200:in
`save_without_validation'
vendor/rails/activerecord/lib/active_record/validations.rb:901:in
`save_without_dirty'
vendor/rails/activerecord/lib/active_record/dirty.rb:75:in
`save_without_transactions'
vendor/rails/activerecord/lib/active_record/transactions.rb:106:in
`save'
vendor/rails/activerecord/lib/active_record/connection_adapters/
abstract/database_statements.rb:66:in `transaction'
vendor/rails/activerecord/lib/active_record/transactions.rb:79:in
`transaction'
vendor/rails/activerecord/lib/active_record/transactions.rb:98:in
`transaction'
vendor/rails/activerecord/lib/active_record/transactions.rb:106:in
`save'
vendor/rails/activerecord/lib/active_record/transactions.rb:118:in
`rollback_active_record_state!'
vendor/rails/activerecord/lib/active_record/transactions.rb:106:in
`save'
app/controllers/cities_controller.rb:39:in `create'
app/controllers/cities_controller.rb:38:in `create'
script\server:3

the City model

class City < ActiveRecord::Base
  belongs_to :country

  validates_presence_of :name,:country
  validates_uniqueness_of :name

  attr_accessor :country

#here sould be all the automation happening
  def before_validation_on_create
    self.country_id = 0
  end
end

Ok so I've figured out some thing
There is a problem between the belongs_to :country
and the attr_accessor :country

The names should be different
I hope from now on it'll be a piece of cacke :wink: