I'm trying to seed a set of football teams and football positions, and rails is strangely simply not doing it.
rake db:seed --trace
** Invoke db:seed (first_time)
** Execute db:seed
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:abort_if_pending_migrations
All looks fine but when I bring up my site, (which was working peachy before, I know it's not the code there) there are no records.
I'm loading three objects: teams, players, and positions. Interestingly, when I got to make a new team, there are no fields, but when I go to make a new player, there ARE fields.
Seeds.rb looks like this:
position = Position.create(:positionName => 'Quarterback', :positionShort => 'QB', :stance => 'offense')
team = Team.create( name:'Patriots', location:'New England', conference:'AFC', division:'East', wins:'5', losses:'3')
Let me know what else you'll need to see, I am at a crossroads of inexperience and an inexplicable error.
The seeds.rb file is where the seed data is stored, but you need to run the appropriate rake task to actually use the seed data. Using rake -T in your project directory shows information about following tasks: rake db:seed. Load the seed data from db/seeds.rb.
Using a custom Rails task to seed actual data To seed actual data, it is best to create a custom Rails task. Let's generate one to add genres. First generate the model and then migrate the database. Finally create the task.
Rails seed files are a useful way of populating a database with the initial data needed for a Rails project. The Rails db/seeds. rb file contains plain Ruby code and can be run with the Rails-default rails db:seed task.
I'm going to make a suggestion with the hope that you'll see an error. In your seeds file use create!
instead of create
. That will cause the operation to throw an exception if the model doesn't meet the constraints. Give that a shot and see if it helps.
position = Position.create!(:positionName => 'Quarterback', :positionShort => 'QB', :stance => 'offense') team = Team.create!( name:'Patriots', location:'New England', conference:'AFC', division:'East', wins:'5', losses:'3')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With