Rails initializers are running while migrating database

别来无恙 提交于 2019-12-04 01:59:37

Here is a solution how to prevent an initializer from running in Rake task:

unless ( File.basename($0) == 'rake')
   # Initializer code
end
Alessandro Gurgel

If your initializer depends on the creaton of a specific table, one alternative is to check using ActiveRecord::Base.connection.table_exists? :mytable.

Matt

Migrates need to load your environment, initializers are an integral part of an environment. If you need an initializer not to run during migrates then it's probably in the wrong place.

If you can't move it elsewhere then perhaps this answer (create a 'fast migrate' rake task) will help.

To get around that problem I did this:

if ActiveRecord::Base.connection.table_exists? :settings # check if table exists
  if Setting.first.present? # check if first record has been seeded
    NUMBERS = Setting.first.my_numbers.split(",")
  else
    NUMBERS = '987654321'
  end
else
  NUMBERS = '123456789'
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!