I am trying to seed my rails 3 app, and hitting a problem. My Category table has the following fields:
string “name”
integer “position”
integer “parent_id”
The parent_id is the id of another category, for nested_set.
Seeds.rb
Category.delete_all
Category.create(:name => "Category 1",
:position => 1,
:parent_id => nil )
Category.create(:name => "Subcategory 1",
:position => 1,
:parent_id => 1 )
When I run
rake db:seed
I get the error:
rake aborted!
Couldn't find Category with id=1
Tasks: TOP => db:seed
(See full trace by running task with --trace)
So, how do I create the Category first so the ID 1 will be available?
The issue with your version is not that it isn’t being created in order, it’s that the autoincrement doesn’t reset so it isn’t given an ID of 1.
Try: