Ruby On Rails: way to create different seeds file for environments

后端 未结 2 1287
孤独总比滥情好
孤独总比滥情好 2021-01-31 04:41

How can one make the task rake db:seed to use different seeds.rb file on production and development?

edit: any better strategy will be welcome

2条回答
  •  北海茫月
    2021-01-31 04:57

    You can have a rake task behave differently based on the current environment, and you can change the environment a task runs in by passing RAILS_ENV=production to the command. Using these two together you could produce something like so:

    Create the following files with your environment specific seeds:

    db/seeds/development.rb
    db/seeds/test.rb
    db/seeds/production.rb
    

    Place this line in your base seeds file to run the desired file

    load(Rails.root.join( 'db', 'seeds', "#{Rails.env.downcase}.rb"))
    

    Call the seeds task:

    rake db:seed RAILS_ENV=production 
    

提交回复
热议问题