0
votes

So I have the following in my seeds.rb file:

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)

and when I run the db:seed in my terminal it throws an error:

rails aborted! ActiveModel::MissingAttributeError: can't write unknown attribute category_id /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/attribute.rb:182:in with_value_from_database' /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/attribute_set.rb:53:inwrite_from_user' /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/attribute_methods/write.rb:50:in 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 write_attribute' /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/attribute_methods.rb:361:in []=' /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/associations/belongs_to_association.rb:76:in replace_keys' /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/associations/belongs_to_association.rb:14:in replace' /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/associations/singular_association.rb:22:in writer' /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/associations/builder/association.rb:119:in category=' /var/lib/gems/2.3.0/gems/activemodel-5.0.2/lib/active_model/attribute_assignment.rb:46:in public_send' /var/lib/gems/2.3.0/gems/activemodel-5.0.2/lib/active_model/attribute_assignment.rb:46:in _assign_attribute' /var/lib/gems/2.3.0/gems/activemodel-5.0.2/lib/active_model/attribute_assignment.rb:40:in block in _assign_attributes' /var/lib/gems/2.3.0/gems/activemodel-5.0.2/lib/active_model/attribute_assignment.rb:39:in each' /var/lib/gems/2.3.0/gems/activemodel-5.0.2/lib/active_model/attribute_assignment.rb:39:in _assign_attributes' /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/attribute_assignment.rb:26:in _assign_attributes' /var/lib/gems/2.3.0/gems/activemodel-5.0.2/lib/active_model/attribute_assignment.rb:33:in assign_attributes' /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/core.rb:319:in initialize' /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/inheritance.rb:65:in new' /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/inheritance.rb:65:in new' /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/persistence.rb:33:in create' /home/vic/railsapps/Swap_Deck/db/seeds.rb:12:in <top (required)>' /var/lib/gems/2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:inload' /var/lib/gems/2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in block in load' /var/lib/gems/2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:259:inload_dependency' /var/lib/gems/2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in load' /var/lib/gems/2.3.0/gems/railties-5.0.2/lib/rails/engine.rb:549:in load_seed' /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/tasks/database_tasks.rb:268:in load_seed' /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/railties/databases.rake:196:in block (2 levels) in ' /var/lib/gems/2.3.0/gems/railties-5.0.2/lib/rails/commands/rake_proxy.rb:14:in block in run_rake_task' /var/lib/gems/2.3.0/gems/railties-5.0.2/lib/rails/commands/rake_proxy.rb:11:in run_rake_task' /var/lib/gems/2.3.0/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:51:in run_command!' /var/lib/gems/2.3.0/gems/railties-5.0.2/lib/rails/commands.rb:18:in ' /home/vic/railsapps/Swap_Deck/bin/rails:9:in require' /home/vic/railsapps/Swap_Deck/bin/rails:9:in' /var/lib/gems/2.3.0/gems/spring-2.0.1/lib/spring/client/rails.rb:28:in load' /var/lib/gems/2.3.0/gems/spring-2.0.1/lib/spring/client/rails.rb:28:in call' /var/lib/gems/2.3.0/gems/spring-2.0.1/lib/spring/client/command.rb:7:in call' /var/lib/gems/2.3.0/gems/spring-2.0.1/lib/spring/client.rb:30:inrun' /var/lib/gems/2.3.0/gems/spring-2.0.1/bin/spring:49:in <top (required)>' /var/lib/gems/2.3.0/gems/spring-2.0.1/lib/spring/binstub.rb:31:in load' /var/lib/gems/2.3.0/gems/spring-2.0.1/lib/spring/binstub.rb:31:in <top (required)>' /home/vic/railsapps/Swap_Deck/bin/spring:15:in ' bin/rails:3:in load' bin/rails:3:in' Tasks: TOP => db:seed (See full trace by running task with --trace)

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.

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.

The migrations code for categories and subcats:

class CreateCategories < 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 < 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
2
Error say that Subcategory model does not have category_id column. Is it true? May you show structure and models code for Category and SubcategoryIlya Lavrov
my models are as follows:class Subcategory < ApplicationRecord belongs_to :category end and:class Category < ApplicationRecord has_many :subcategories endVic Alfieri
Does my answer resolved the issue?Pavan
No, there were a whole bunch of other issues that came up. So I had to get help from some of my real life helpers as I said below.Vic Alfieri

2 Answers

0
votes

rails aborted! ActiveModel::MissingAttributeError: can't write unknown attribute category_id

The error tells that you are missing category_id attribute in subcategories table. make sure you have that to fix the error.

0
votes

Looking at your seeds I assume that a category can have multiple subcategories.

In this case your migrations should be:

class CreateCategories < ActiveRecord::Migration[5.0]
  def change
    create_table :categories do |t|
      t.string :category_name
      t.timestamps
    end
  end
end


class CreateSubcategories < ActiveRecord::Migration[5.0]
  def change
    create_table :subcategories do |t|
      t.string :subcategory_name
      t.references :category, foreign_key: true
      t.timestamps
    end
  end
end

You do not need to specify category_id and subcategory_id as the migrations will automatically generate a primary key column named id.

Full details of how to use migrations are specified here

and your models should be:

class Category < ApplicationRecord
  has_many :subcategories
end

class Subcategory < ApplicationRecord
  belongs_to :category
end

In addition I suggest you rename the category_name to name and subcategory_name to name. The use of prefix is redundant.