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

后端 未结 2 1289
孤独总比滥情好
孤独总比滥情好 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 05:10

    I like to implement all seeds inside one seed.rb file and then just separate the environments inside.

    if Rails.env.production? 
      State.create(state: "California", state_abbr: "CA")
      State.create(state: "North Dakota", state_abbr: "ND")
    end
    
    if Rails.env.development?
      for 1..25
        Orders.create(order_num: Faker::Number:number(8), order_date: Faker::Business.credit_card_expiry_date)
      end
    end
    

    That way you do not need to cast the RAILS_ENV property on your rake task, or manage multiple files. You also can include Rails.env.test?, but I personally let RSPEC take care of the testing data.

提交回复
热议问题