title: ActiveModel::MissingAttributeError: can't write unknown attribute `user_id`
body: <p>I am trying to implement a &quot;comment&quot; feature as part of my assignment for an project I am building.</p>
  <p>Earlier in the course we created a comment table and had the Faker gem generate fake comments.</p>
  <p>My instructions are as follows:</p>
  <blockquote>
  <p>Comments must be associated with users, so add a user_id foreign key to the comments table. Remember to add an index to it too;</p>
  <p>Update the User model so you can call user.comments, and the Comment model so you can call comment.user;</p>
  <p>Modify the seeds.rb file to create valid comments when you run db:reset;</p>
  </blockquote>
  <p>Initially I tried to run my rails generate commands but kept running into this error:</p>
  <pre><code>▶ rake db:migrate
  == 20150508143445 CreateComments: migrating ===================================
  -- create_table(:comments)
  rake aborted!
  StandardError: An error has occurred, this and all later migrations canceled:
  
  SQLite3::SQLException: table &quot;comments&quot; already exists: CREATE TABLE &quot;comments&quot; (&quot;id&quot; INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, &quot;description&quot; text, &quot;created_at&quot; datetime NOT NULL, &quot;updated_at&quot; datetime NOT NULL) /Users/jon/code/bloccit/db/migrate/20150508143445_create_comments.rb:3:in `change'
  -e:1:in `&lt;main&gt;'
  ActiveRecord::StatementInvalid: SQLite3::SQLException: table &quot;comments&quot; already exists: CREATE TABLE &quot;comments&quot; (&quot;id&quot; INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, &quot;description&quot; text, &quot;created_at&quot; datetime NOT NULL, &quot;updated_at&quot; datetime NOT NULL) 
  /Users/jon/code/bloccit/db/migrate/20150508143445_create_comments.rb:3:in `change'
  -e:1:in `&lt;main&gt;'
  SQLite3::SQLException: table &quot;comments&quot; already exists
  /Users/jon/code/bloccit/db/migrate/20150508143445_create_comments.rb:3:in `change'
  -e:1:in `&lt;main&gt;'
  Tasks: TOP =&gt; db:migrate
  (See full trace by running task with --trace)
  </code></pre>
  <p>So I discovered that I needed to delete the old table from the start of the course. I did this in the console.</p>
  <pre><code>ActiveRecord::Base.connection.execute(&quot;drop table comments&quot;)
  </code></pre>
  <p>It seemed to work. I was then able to run these in terminal.</p>
  <pre><code>▶ rake db:migrate
  == 20150508143445 CreateComments: migrating   ===================================
  -- create_table(:comments)
     -&gt; 0.0015s
  == 20150508143445 CreateComments: migrated (0.0016s) ==========================
  
  == 20150508152354 DropComments: migrating =====================================
  == 20150508152354 DropComments: migrated (0.0000s) ============================
  
  ▶ rails g migration AddCommentToUsers commed_id:integer:index
    invoke  active_record
    create    db/migrate/20150508155200_add_comment_to_users.rb
  
  ▶ rake db:migrate
  == 20150508155200 AddCommentToUsers: migrating ================================
  -- add_column(:users, :commed_id, :integer)
     -&gt; 0.0010s
  -- add_index(:users, :commed_id)
     -&gt; 0.0012s
  == 20150508155200 AddCommentToUsers: migrated (0.0023s) =======================
  </code></pre>
  <p><strong>models/comment.rb</strong></p>
  <pre><code>class Comment &lt; ActiveRecord::Base
    belongs_to :user
  end
  </code></pre>
  <p><strong>models/user.rb</strong></p>
  <pre><code>class User &lt; ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable, :confirmable
    has_many :posts
    has_many :comments
  
    # CarrierWave method for attribute functionality
    mount_uploader :avatar, AvatarUploader
  
    # These methods check the role of a user in the database
    def admin?
      role == 'admin'
    end
  
    def moderator?
      role == 'moderator'
    end
  end
  </code></pre>
  <p><strong>migrations/create_comments.rb</strong></p>
  <pre><code>class CreateComments &lt; ActiveRecord::Migration
    def change
      create_table :comments do |t|
        t.text :description
  
        t.timestamps null: false
      end
    end
  end
  </code></pre>
  <p><strong>migrations/add_comment_to_users.rb</strong></p>
  <pre><code>class AddCommentToUsers &lt; ActiveRecord::Migration
    def change
      add_column :users, :commed_id, :integer
      add_index :users, :commed_id
    end
  end
  </code></pre>
  <p>In my <strong>seeds.rb</strong> I have changed</p>
  <pre><code># Create Comments
  50.times do 
    Comment.create!(
        # user: users.sample, # we have not yet associated Users with Comments
      post: posts.sample,
      description: Faker::Lorem.paragraph
      )
  end
  </code></pre>
  <p>to</p>
  <pre><code># Create Comments
  50.times do 
    Comment.create!(
      user: users.sample,
      description: Faker::Lorem.paragraph
      )
  end
  </code></pre>
  <p>Now when I run <strong>rake db:reset</strong> I get this error.</p>
  <pre><code>▶ rake db:reset
  -- create_table(&quot;advertisements&quot;, {:force=&gt;:cascade})
     -&gt; 0.0047s
  -- create_table(&quot;answers&quot;, {:force=&gt;:cascade})
     -&gt; 0.0010s
  -- add_index(&quot;answers&quot;, [&quot;question_id&quot;], {:name=&gt;&quot;index_answers_on_question_id&quot;})
     -&gt; 0.0014s
  -- create_table(&quot;comments&quot;, {:force=&gt;:cascade})
     -&gt; 0.0010s
  -- create_table(&quot;posts&quot;, {:force=&gt;:cascade})
     -&gt; 0.0010s
  -- add_index(&quot;posts&quot;, [&quot;topic_id&quot;], {:name=&gt;&quot;index_posts_on_topic_id&quot;})
     -&gt; 0.0010s
  -- add_index(&quot;posts&quot;, [&quot;user_id&quot;], {:name=&gt;&quot;index_posts_on_user_id&quot;})
     -&gt; 0.0014s
  -- create_table(&quot;questions&quot;, {:force=&gt;:cascade})
     -&gt; 0.0010s
  -- create_table(&quot;summaries&quot;, {:force=&gt;:cascade})
     -&gt; 0.0010s
  -- add_index(&quot;summaries&quot;, [&quot;post_id&quot;], {:name=&gt;&quot;index_summaries_on_post_id&quot;})
     -&gt; 0.0010s
  -- create_table(&quot;topics&quot;, {:force=&gt;:cascade})
     -&gt; 0.0036s
  -- create_table(&quot;users&quot;, {:force=&gt;:cascade})
     -&gt; 0.0015s
  -- add_index(&quot;users&quot;, [&quot;commed_id&quot;], {:name=&gt;&quot;index_users_on_commed_id&quot;})
     -&gt; 0.0009s
  -- add_index(&quot;users&quot;, [&quot;comment_id&quot;], {:name=&gt;&quot;index_users_on_comment_id&quot;})
     -&gt; 0.0012s
  -- add_index(&quot;users&quot;, [&quot;email&quot;], {:name=&gt;&quot;index_users_on_email&quot;, :unique=&gt;true})
     -&gt; 0.0015s
  -- add_index(&quot;users&quot;, [&quot;reset_password_token&quot;], {:name=&gt;&quot;index_users_on_reset_password_token&quot;, :unique=&gt;true})
     -&gt; 0.0017s
  -- initialize_schema_migrations_table()
     -&gt; 0.0034s
  rake aborted!
  ActiveModel::MissingAttributeError: can't write unknown attribute `user_id`
  /Users/jon/code/Bloccit/db/seeds.rb:45:in `block in &lt;top (required)&gt;'
  /Users/jon/code/Bloccit/db/seeds.rb:44:in `times'
  /Users/jon/code/Bloccit/db/seeds.rb:44:in `&lt;top (required)&gt;'
  -e:1:in `&lt;main&gt;'
  Tasks: TOP =&gt; db:setup =&gt; db:seed
  (See full trace by running task with --trace)
  </code></pre>
  <p>Here is the full stack trace:</p>
  <pre><code>ActiveModel::MissingAttributeError: can't write unknown attribute `user_id`
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute.rb:138:in `with_value_from_database'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_set.rb:39:in `write_from_user'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods/write.rb:74:in `write_attribute_with_type_cast'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods/write.rb:56:in `write_attribute'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods/dirty.rb:96:in `write_attribute'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods.rb:373:in `[]='
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/associations/belongs_to_association.rb:83:in `replace_keys'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/associations/belongs_to_association.rb:14:in `replace'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/associations/singular_association.rb:17:in `writer'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/associations/builder/association.rb:123:in `user='
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:54:in `public_send'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:54:in `_assign_attribute'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:41:in `block in assign_attributes'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:35:in `each'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:35:in `assign_attributes'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/core.rb:559:in `init_attributes'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/core.rb:281:in `initialize'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/inheritance.rb:61:in `new'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/inheritance.rb:61:in `new'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/persistence.rb:50:in `create!'
  /Users/jon/code/Bloccit/db/seeds.rb:45:in `block in &lt;top (required)&gt;'
  /Users/jon/code/Bloccit/db/seeds.rb:44:in `times'
  /Users/jon/code/Bloccit/db/seeds.rb:44:in `&lt;top (required)&gt;'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `block in load'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/engine.rb:547:in `load_seed'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/railties/databases.rake:180:in `block (2 levels) in &lt;top (required)&gt;'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:240:in `call'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:240:in `block in execute'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:235:in `each'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:235:in `execute'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:179:in `block in invoke_with_call_chain'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:201:in `block in invoke_prerequisites'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:199:in `each'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:199:in `invoke_prerequisites'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:178:in `block in invoke_with_call_chain'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:165:in `invoke'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/railties/databases.rake:139:in `block (2 levels) in &lt;top (required)&gt;'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:240:in `call'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:240:in `block in execute'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:235:in `each'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:235:in `execute'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:179:in `block in invoke_with_call_chain'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:165:in `invoke'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:150:in `invoke_task'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `block (2 levels) in top_level'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `each'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `block in top_level'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:115:in `run_with_threads'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:100:in `top_level'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:78:in `block in run'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:75:in `run'
  /Users/jon/code/Bloccit/bin/rake:8:in `&lt;top (required)&gt;'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `block in load'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
  /Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
  -e:1:in `&lt;main&gt;'
  Tasks: TOP =&gt; db:setup =&gt; db:seed
  </code></pre>
  <p>I am stumped here. I thought by deleting the old table and running the new generate commands + migrating everything would be in order. Apparently this is not the case.</p>
  <p>What am I doing wrong here?</p>
  <p><strong>edited to include line 45 of my seeds.rb</strong></p>
  <pre><code>Comment.create!(
  </code></pre>