kpaul kpaul - 1 year ago 283
Ruby Question

Cannot seed data in production using heroku

I had posted this question before but I haven't got any answers that solved my problem. Therefore, I am reposting this question.

I am using heroku to deploy my rails app. I am trying to seed some data in the production by running the command

heroku run rake db:seed.
However, the command is not working. I am not able to seed files in production. The
file is given below.

#This file should contain all the record creation needed to seed the database with its default values.
#The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).


#cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
#Mayor.create(name: 'Emanuel', city: cities.first)


5.times do
Scoreboard.create!(name_of_scoreboard: "scoreboard_abc",
name_of_organization: "abcdef",
name_of_activity: "ghijklmn",
user_id: 1,
states: "state",
country: "state",
cities: "state")

$ heroku run rake db:seed
Running rake db:seed on ⬢ app... up, run.4751
ActiveRecord::SchemaMigration Load (2.0ms) SELECT "schema_migrations".* FROM "schema_migrations"

I run
heroku restart
but the objects aren't seeded in the production database. I have tried seeding this file in development and it works perfectly. I am not sure what's wrong.

The heroku logs -t file is given below.

2016-06-22T00:50:58.882699+00:00 heroku[api]: Starting process with command `bundle exec rake db:seed` by *******
2016-06-22T00:51:07.986301+00:00 heroku[run.1041]: Awaiting client
2016-06-22T00:51:08.026539+00:00 heroku[run.1041]: Starting process with command `bundle exec rake db:seed`
2016-06-22T00:51:08.157630+00:00 heroku[run.1041]: State changed from starting to up
2016-06-22T00:51:12.891248+00:00 heroku[run.1041]: State changed from up to complete
2016-06-22T00:51:12.881329+00:00 heroku[run.1041]: Process exited with status 0

I have the database.yml file and seeds.rb files as part of gitignore. I am not sure if that might be the cause of the problem. I am pretty sure its not.

Answer Source

seeds.rb should be in the repository in order for rails to seed it. If there is no seeds.rb, rails doesn't complain that the file is missing, instead it just completes the rake db:seed task with out any error.

So make sure you remove the seeds.rb from the .gitignore, add it to the git with git add --force db/seeds.rb and git push it to the repository.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download