db:seed is throwing an error

2019-07-04 12:02发布

问题:

I'm using ruby 1.9.2 with rails 3.0.9.

Whenever I try to execute rake db:seed it throws following error:

rake aborted!
uninitialized constant EmployeeCategory

I've disabled threadsafe and enabled 'dependency_loading in config/application.rb file.

config.threadsafe! unless $rails_rake_task
config.dependency_loading = true

But it is still not working.

Here's the content of seed.rb file

StudentCategory.destroy_all
StudentCategory.create([
 {:name=>"OBC",:is_deleted=>false},
 {:name=>"General",:is_deleted=>false}
])

EmployeeCategory.create([
{:name => 'Management',:prefix => 'MGMT',:status => true},
{:name => 'Teaching',:prefix => 'TCR',:status => true},
{:name => 'Fedena Admin',:prefix => 'Admin',:status => true},
{:name => 'Non-Teaching',:prefix => 'NTCR',:status => true}
])
EmployeePosition.create([
{:name => 'Principal',:employee_category_id => 2,:status => true},
{:name => 'Jr.Teacher',:employee_category_id => 3,:status => true},
{:name => 'Clerk',:employee_category_id => 4,:status => true}
])

回答1:

This may be an obvious one, but do you have your employee_category.rb model created in /models? Found that every time I get this error I create the view, controller, and route, but forget something simple like adding the model file.



回答2:

I solved this by making sure I was referencing my model, not my controller in my seed.db data. My controller is named Categories, and my model is named category.

This did not work:

categories = []
categories << Categories.create(name: 'guitar')
categories << Categories.create(name: 'bass')
categories << Categories.create(name: 'synth')
categories << Categories.create(name: 'effects pedal')
categories << Categories.create(name: 'amplifier')


This worked:

categories = []
categories << Category.create(name: 'guitar')
categories << Category.create(name: 'bass')
categories << Category.create(name: 'synth')
categories << Category.create(name: 'effects pedal')
categories << Category.create(name: 'amplifier')


回答3:

Unrelated but when the model ends with 's' like Status, you also get error. I faced the same problem so this might help some googlers



回答4:

Did you create model classes in individual file for each? I used to put two classes in a single file (e.g. a header and details relation classes), thought it would be easier to manage them but that was the cause db:seed failed!