I have the following models:
class Property < ApplicationRecord
# Other validations
has_one :address
accepts_nested_attributes_for :address, update_only: true
end
class Address < ApplicationRecord
has_one :country
has_one :state
has_one :city
has_one :suburb
belongs_to :property
end
The relationships for country
, state
, city
and suburb
are all linked with a has_many
and belongs_to
between each other.
The problem is :
On my properties/_form.html.erb
file I'm trying to create the address with nested fields_for
the :address
and on those nested fields using options_from_collection_for_select
. Code below:
<fieldset>
<div class="form-group">
<%= form.label :address, "Dirección", class: "col-sm-2 control-label"%>
<div class="col-sm-9">
<%= form.fields_for :address do |ff| %>
<div class="row">
<div class="col-sm-4">
<select class="form-control" name="property[address_attributes][country_id]" >
<%= options_from_collection_for_select(Country.all, :id, :name) %>
</select>
</div>
<div class="col-sm-4">
<select class="form-control" name="property[address_attributes][state_id]" >
<%= options_from_collection_for_select(State.all, :id, :name) %>
</select>
</div>
<div class="col-sm-4">
<select class="form-control" name="property[address_attributes][city_id]" >
<%= options_from_collection_for_select(City.all, :id, :name) %>
</select>
And getting this error on submit:
Update 1
So I've changed my form a bit with this:
<%= ff.select :country, options_from_collection_for_select(Country.all, :id, :name) %>
On all of my relationships and now the error I get is:
Country(#70194352893700) expected, got "1" which is an instance of String(#70194311847460)
Update 2:
Here is the code on my schema.rb
describing my addresses
table:
create_table "addresses", force: :cascade do |t|
t.string "street"
t.integer "number"
t.integer "zip_code"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "property_id"
t.integer "countries_id"
t.integer "states_id"
t.integer "cities_id"
t.integer "suburbs_id"
t.index ["cities_id"], name: "index_addresses_on_cities_id"
t.index ["countries_id"], name: "index_addresses_on_countries_id"
t.index ["property_id"], name: "index_addresses_on_property_id"
t.index ["states_id"], name: "index_addresses_on_states_id"
t.index ["suburbs_id"], name: "index_addresses_on_suburbs_id"
end