0

在命令行脚本中引导 Rails 需要很长时间(2.5 秒以上),例如:

timer = Time.now  
ENV["RAILS_ENV"] ||= "development"  
require File.expand_path('../../config/application', __FILE__)  
Rails.application.require_environment!  
puts "Init complete in #{Time.now - timer} sec"  
# -> Init complete in 2.621531 sec  

我错过了什么吗?如果不是,我真的需要引导所有东西来访问模型吗?

4

1 回答 1

0

I don't have a lot of experience with this, but perhaps you should take a look at trimming down the number of libraries that are required in application.rb:

# require 'rails/all' # remove this and only require what you need
require 'activerecord/railtie' # this may be all you need

Additionally, take a look at the gems you are loading and see if any of them are taking an inordinate amount of time. If a particular gem is slowing down boot time, simply add:

# you will need to explicitly load the gem when needed,
# but it won't be required at boot time 
gem some_gem, :require => false

You could create a special environment that you run the script in and customize the boot process for that environment.

You should profile that block of code and see where most of the time is spent.

What exactly are you trying to get out of booting rails? Access to the models? the underlying data? Perhaps there's an alternate approach than booting all of rails.

于 2013-03-07T06:02:33.900 回答