A012faeaa44311fd38b97905c6f30292

在 Rails 之外使用 ActiveRecord

2012-02-02 12:52:03 +0800yjchenxRuby on Rails 節點 中發起
最後由 hechian2012-02-02 16:50:27 +0800回應 , 196次閱讀

我用 Rails 管理資料,但用 nanoc 產生靜態網頁,再上傳到 Google App Engine 做免費代管。對個人網站來說算是很好的方案。為了從 nanoc 中讀取 Rails 的資料庫,可以使用以下的程式碼:

require 'active_record'
require 'sqlite3'

RAILS_ROOT ||= "/path_to_your_rails_app"
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3',
:database => "#{RAILS_ROOT}/db/production.sqlite3")

Dir.glob("#{RAILS_ROOT}/app/models/*.rb").each do |r| require r end
Dir.glob("#{RAILS_ROOT}/app/models//.rb").each do |r| require r end

如此就可以直接使用 Models。

(Originally posted at http://blog.berylliumwork.com/2012/01/use-activerecord-without-rails.html)

截至 2012-02-02 16:50:27 +0800,共收到 1 條回應
Ff7afae5b23125e25e18ad8c0404bd78
hechian 1樓, 於2012-02-02 16:50:26 +0800回應

這個不知道會不會有connection pool的問題 ...
之前用這種方式來做,但是Connection Pool很容易炸掉

需要 登入 後方可回應,如果你還沒有帳號按這裡 註冊