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:inwith_value_from_database' /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/attribute_set.rb:53:in
write_from_user' /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/attribute_methods/write.rb:50:inwrite_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:inreplace' /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:incategory=' /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:ineach' /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:ininitialize' /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:innew' /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:in
load' /var/lib/gems/2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:inblock in load' /var/lib/gems/2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:259:in
load_dependency' /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/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:inload_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:inblock 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:inrun_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:inrequire' /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:inload' /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:incall' /var/lib/gems/2.3.0/gems/spring-2.0.1/lib/spring/client.rb:30:in
run' /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:inload' 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