Ruby on rails, run the method on the server 2.3.

I want to run the method when starting the rails server. This is a model method.

I tried using config / initializers / myfile.rb, but this method was called during the migration, so it was selected from a nonexistent table.

I also tried environment.rb, but the class does not exist yet (and will probably have the same problem with migrations)

I do not know where to place this method, so it will only start when the server starts, and not during the migration.

+3
source share
4 answers

There are a few things you could do to improve this a bit. The problem is that you run this code when rake loads your environment, but you really want to run it only when the environment loads with an instance of your web server. One way around this is to set the value when rake loads your environment and when this value is set so as not to execute the initialization code. You can do it as follows:

task :environment => :disable_initializer task :disable_initializer do ENV['DISABLE_INITIALIZER_FROM_RAKE'] = 'true' end #In your initializer: ENV['DISABLE_INITIALIZER_FROM_RAKE'] || MyModel.method_call 
+3
source

It is impossible to avoid this from my understanding. You can put the initializer code, which relies on the new table in the disaster recovery unit, to calm the situation so that others can migrate.

+2
source

Try putting the method call in boot.rb in the startup method after calling Rails :: initializer. I have no rails in front of me right now because I'm at work, but I think that the whole environment should be loaded with this item, and you can run methods within the framework.

+1
source

I found this to work very well:

if File.basename ($ 0) == "rails" && & && ARGV == []

He also discovers that "rails are generated .."

0
source

All Articles