title: seeds.rb rails db:seed not working
body: <p>So I have the following in my seeds.rb file:</p>
  
  <pre><code>pop_culture = Category.create(category_name: 'Pop Culture')
  sports = Category.create(category_name: 'Sports')
  tazos = Category.create(category_name: 'Tazos')
  woolworths = Category.create(category_name: 'Woolworths')
  Subcategory.create(subcategory_name: 'AFL', category: sports)
  Subcategory.create(subcategory_name: 'NRL', category: sports)
  Subcategory.create(subcategory_name: 'Basketball', category: sports)
  Subcategory.create(subcategory_name: 'Baseball', category: sports)
  Subcategory.create(subcategory_name: 'Cricket', category: sports)
  Subcategory.create(subcategory_name: 'Ninja Turtles', category: pop_culture)
  Subcategory.create(subcategory_name: 'Pokemon', category: pop_culture)
  Subcategory.create(subcategory_name: 'Final Fantasy', category: pop_culture)
  Subcategory.create(subcategory_name: 'Marvel Cards', category: pop_culture)
  Subcategory.create(subcategory_name: 'DC', category: pop_culture)
  Subcategory.create(subcategory_name: 'Yu-gi-oh', category: pop_culture)
  Subcategory.create(subcategory_name: 'Looney Tunes', category: tazos)
  Subcategory.create(subcategory_name: 'Cheetahs', category: tazos)
  Subcategory.create(subcategory_name: 'Star Wars', category: tazos)
  Subcategory.create(subcategory_name: 'Simpsons', category: tazos)
  Subcategory.create(subcategory_name: 'Space Jam', category: tazos)
  Subcategory.create(subcategory_name: 'Dreamworks', category: woolworths)
  Subcategory.create(subcategory_name: 'Disney Dominoes', category: woolworths)
  Subcategory.create(subcategory_name: 'Marvel Discs', category: woolworths)
  </code></pre>
  
  <p>and when I run the <code>db:seed</code> in my terminal it throws an error: </p>
  
  <blockquote>
    <p>rails aborted! ActiveModel::MissingAttributeError: can't write unknown
    attribute <code>category_id</code>
    /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/attribute.rb:182:in
    <code>with_value_from_database'
    /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/attribute_set.rb:53:in</code>write_from_user'
    /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/attribute_methods/write.rb:50:in
    <code>write_attribute_with_type_cast'
    /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/attribute_methods/write.rb:32:in
    </code>write_attribute'
    /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/attribute_methods.rb:361:in
    <code>[]='
    /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/associations/belongs_to_association.rb:76:in
    </code>replace_keys'
    /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/associations/belongs_to_association.rb:14:in
    <code>replace'
    /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/associations/singular_association.rb:22:in
    </code>writer'
    /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/associations/builder/association.rb:119:in
    <code>category='
    /var/lib/gems/2.3.0/gems/activemodel-5.0.2/lib/active_model/attribute_assignment.rb:46:in
    </code>public_send'
    /var/lib/gems/2.3.0/gems/activemodel-5.0.2/lib/active_model/attribute_assignment.rb:46:in
    <code>_assign_attribute'
    /var/lib/gems/2.3.0/gems/activemodel-5.0.2/lib/active_model/attribute_assignment.rb:40:in
    </code>block in _assign_attributes'
    /var/lib/gems/2.3.0/gems/activemodel-5.0.2/lib/active_model/attribute_assignment.rb:39:in
    <code>each'
    /var/lib/gems/2.3.0/gems/activemodel-5.0.2/lib/active_model/attribute_assignment.rb:39:in
    </code>_assign_attributes'
    /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/attribute_assignment.rb:26:in
    <code>_assign_attributes'
    /var/lib/gems/2.3.0/gems/activemodel-5.0.2/lib/active_model/attribute_assignment.rb:33:in
    </code>assign_attributes'
    /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/core.rb:319:in
    <code>initialize'
    /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/inheritance.rb:65:in
    </code>new'
    /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/inheritance.rb:65:in
    <code>new'
    /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/persistence.rb:33:in
    </code>create' /home/vic/railsapps/Swap_Deck/db/seeds.rb:12:in <code>&lt;top
    (required)&gt;'
    /var/lib/gems/2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in</code>load'
    /var/lib/gems/2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in <code>block in load'
    /var/lib/gems/2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:259:in</code>load_dependency'
    /var/lib/gems/2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in <code>load'
    /var/lib/gems/2.3.0/gems/railties-5.0.2/lib/rails/engine.rb:549:in
    </code>load_seed'
    /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/tasks/database_tasks.rb:268:in
    <code>load_seed'
    /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/railties/databases.rake:196:in
    </code>block (2 levels) in '
    /var/lib/gems/2.3.0/gems/railties-5.0.2/lib/rails/commands/rake_proxy.rb:14:in
    <code>block in run_rake_task'
    /var/lib/gems/2.3.0/gems/railties-5.0.2/lib/rails/commands/rake_proxy.rb:11:in
    </code>run_rake_task'
    /var/lib/gems/2.3.0/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:51:in
    <code>run_command!'
    /var/lib/gems/2.3.0/gems/railties-5.0.2/lib/rails/commands.rb:18:in
    </code>' /home/vic/railsapps/Swap_Deck/bin/rails:9:in
    <code>require' /home/vic/railsapps/Swap_Deck/bin/rails:9:in</code>'
    /var/lib/gems/2.3.0/gems/spring-2.0.1/lib/spring/client/rails.rb:28:in
    <code>load'
    /var/lib/gems/2.3.0/gems/spring-2.0.1/lib/spring/client/rails.rb:28:in
    </code>call'
    /var/lib/gems/2.3.0/gems/spring-2.0.1/lib/spring/client/command.rb:7:in
    <code>call'
    /var/lib/gems/2.3.0/gems/spring-2.0.1/lib/spring/client.rb:30:in</code>run'
    /var/lib/gems/2.3.0/gems/spring-2.0.1/bin/spring:49:in <code>&lt;top
    (required)&gt;'
    /var/lib/gems/2.3.0/gems/spring-2.0.1/lib/spring/binstub.rb:31:in
    </code>load'
    /var/lib/gems/2.3.0/gems/spring-2.0.1/lib/spring/binstub.rb:31:in
    <code>&lt;top (required)&gt;' /home/vic/railsapps/Swap_Deck/bin/spring:15:in
    </code>' bin/rails:3:in <code>load' bin/rails:3:in</code>'
    Tasks: TOP => db:seed (See full trace by running task with --trace)</p>
  </blockquote>
  
  <p>So, is there something I need to fix? I can get it to seed just the categories and that works, but I need the subcategories as well, as I want them for a drop-down on my site.</p>
  
  <p>I've been trying to get this stupid thing to work for ages, but I can't get my head around the whole category/subcategory thing.</p>
  
  <p>The migrations code for categories and subcats:</p>
  
  <pre><code>class CreateCategories &lt; ActiveRecord::Migration[5.0]
    def change
      create_table :categories do |t|
        t.integer :category_id
        t.string :category_name
        t.references :subcategory_id, foreign_key: true
  
        t.timestamps
      end
    end
  end
  
  class CreateSubcategories &lt; ActiveRecord::Migration[5.0]
    def change
      create_table :subcategories do |t|
        t.integer :subcategory_id
        t.string :subcategory_name
        t.references :category_id, foreign_key: true
  
        t.timestamps
      end
    end
  end
  </code></pre>