In Rails, why do I get a "can't write unknown attribute" when I try and save my model with non-nil state and country member fields? <p>I’m using Rails 4.2.3. I have the following model …</p> <pre><code>class MyObjectTime &lt; ActiveRecord::Base belongs_to :my_object has_one :state has_one :country </code></pre> <p>I’m having an issue when I try and save my entity with non-empty state and country objects. I have</p> <pre><code> state = State.find_by_iso_and_country_id(hometown_parts[1].strip!, us_country.id) country = state.country my_object_time = MyObjectTime.new(:name =&gt; my_object_time[2], :age =&gt; my_object_time[7], :time_in_ms =&gt; time_in_ms, :overall_rank =&gt; my_object_time[1], :city =&gt; city, :state =&gt; state, :country =&gt; country, :gender_rank =&gt; my_object_time[7], :my_object_id =&gt; my_object_id) </code></pre> <p>but unless both “state” and “country” are nil, I get the error</p> <pre><code>ActiveModel::MissingAttributeError can't write unknown attribute `my_object_time_id` </code></pre> <p>Everything will save fine if both these fields are nil. What’s going on and how can I fix it?</p> <p><b>Edit:</b> Here is the PostGres description of the table in question</p> <pre><code>davedb=&gt; \d my_object_times; Table "public.my_object_times" Column | Type | Modifiers ----------------+-----------------------------+--------------------------------------------------------- id | integer | not null default nextval('my_object_times_id_seq'::regclass) my_object_id | integer | not null first_name | character varying | last_name | character varying | time_in_ms | bigint | created_at | timestamp without time zone | not null updated_at | timestamp without time zone | not null name | character varying | age | integer | city | character varying | state_id | integer | country_id | integer | overall_rank | integer | age_group_rank | integer | gender_rank | integer | Indexes: "my_object_times_pkey" PRIMARY KEY, btree (id) "index_my_object_times_on_country_id" btree (country_id) "index_my_object_times_on_my_object_id" btree (my_object_id) "index_my_object_times_on_state_id" btree (state_id) Foreign-key constraints: "fk_rails_0fe1d25967" FOREIGN KEY (country_id) REFERENCES countries(id) "fk_rails_a8771b3575" FOREIGN KEY (state_id) REFERENCES states(id) "fk_rails_ba656ceafa" FOREIGN KEY (my_object_id) REFERENCES my_bojects(id) ON DELETE CASCADE Referenced by: TABLE "user_my_object_time_matches" CONSTRAINT "fk_rails_2e7860946c" FOREIGN KEY (my_object_time_id) REFERENCES my_object_times(id) </code></pre>