首页 > Rails3.2生产环境预编译与Active Record初始化问题

Rails3.2生产环境预编译与Active Record初始化问题

部署环境:

Ubuntu Server 12.04
Postgresql 9.1
Rails 3.2.14
Capistrano 2.13.5

其中:
pg_hba.conf文件中描述如下:

local    all    postrgesql    truse
local    all    all           md5
local    all    127.0.0.1/32  md5
local    all    ::1/128       md5

config/deploy.rb中有一行:

run "cd #{latest_release} && #{rake} RAILS_ENV=#{rails_env} #{asset_env} assets:precompile"

在远程执行cap staging:deploy时,运行到上面那行代码就会报错,提示无法连接数据库,在Active Record初始化时出现错误。

当时我注释掉那一行,ssh到服务器上,直接cd到项目中执行预编译,也能成功,不会出现错误。

后来我在config/application.rb中加上了:

config.assets.initialize_on_precompile = false

# Heroku中部署Rails3.2程序也有这个要求

最终问题解决。

我的问题是:Rails3.2程序在production环境中执行预编译的时候,真的有初始化Active Record这一步么,以及为什么?


https://devcenter.heroku.com/articles/rails-asset-pipeline


  config.assets.initialize_on_precompile = false # 默认为True

此配置项的作用正是在执行asset:precompile不初始化App。初始化Active Record属于初始化整个APP的一部分。

Rails Guides中有说:

If you set config.assets.initialize_on_precompile to false, be sure to test rake assets:precompile locally before deploying. It may expose bugs where your assets reference application objects or methods, since those are still in scope in development mode regardless of the value of this flag. Changing this flag also affects engines. Engines can define assets for precompilation as well. Since the complete environment is not loaded, engines (or other gems) will not be loaded, which can cause missing assets.

意即如果设置成false,可能导致在编译部分文件时出现Bug(例如:内嵌的erb语句<%= something... %>)。建议在部署前测试。

【热门文章】
【热门文章】