I have an app built on Sinatra. It's supported by several rake tasks that must be run t开发者_如何学Co setup database, etc... The rakefile looks like this
require 'rubygems'
require 'rake'
require 'rake/clean'
require 'rake/gempackagetask'
require 'rake/rdoctask'
require 'rake/testtask'
require File.dirname(__FILE__) + '/lib/config.rb'
require File.dirname(__FILE__) + '/lib/autoloads.rb'
@db = Mongo::Connection.from_uri(settings.db_uri).db(settings.db_uri[Regexp.new('[^/]*$')])
@cache = MemCache.new(settings.cache_server)
Works well on my development machine. Now, the problem is that after deployment to Heroku, the settings
object becomes undefined, so every task fails. Do you have any idea how to solve this?
It looks like the file where your settings are defined is not being loaded. In your config.ru
change require 'my_sinatra_app'
with require './my_sinatra_app'
. Also see this question.
I ran into this same problem when building a sinatra app - I wanted to run some rake tasks to perform db setup operations.
What I ended up doing is abstracting database connection settings, connecting, and schema operations into a separate class. This class is then accessible by the sinatra app and the Rakefile.
My rakefile is here: https://github.com/brighterplanet/status/blob/master/Rakefile#L6 From there, check out lib/bp_status/db.rb
精彩评论