2012-12-04 94 views
1

我發現another similar question並認爲我遵循here中提到的指示,但我仍然遇到此錯誤。mongoid未初始化常量CarrierDomain(NameError)

根據instructions我打電話給Mongoid.load!("path/to/your/mongoid.yml")在我的ruby腳本中,所以我不認爲是這樣。

也許,根據來自inflector的錯誤消息,這是我如何定義CarrierDomain的問題。我從Sinatra腳本引用了一個軌道模型。它位於一個名爲carrier_domain.rb的模型文件中,並且該類被定義爲CarrierDomain(class CarrierDomain)。事實上,這裏是整個類定義(這是一個稍一):

class CarrierDomain 
    include Mongoid::Document 
    embedded_in :carrier 
    field :country 
    field :name 
    field :prefix, default: '' 
end 

這裏的錯誤消息,我得到:

/Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/inflector/methods.rb:230:in `block in constantize': uninitialized constant CarrierDomain (NameError) 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/inflector/methods.rb:229:in `each' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/inflector/methods.rb:229:in `constantize' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/core_ext/string/inflections.rb:54:in `constantize' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.10/lib/mongoid/relations/metadata.rb:606:in `klass' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.10/lib/mongoid/relations/builder.rb:39:in `klass' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.10/lib/mongoid/relations/builders/embedded/many.rb:25:in `block in build' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.10/lib/mongoid/relations/builders/embedded/many.rb:23:in `each' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.10/lib/mongoid/relations/builders/embedded/many.rb:23:in `build' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.10/lib/mongoid/relations/accessors.rb:43:in `create_relation' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.10/lib/mongoid/relations/accessors.rb:26:in `__build__' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.10/lib/mongoid/relations/accessors.rb:156:in `block (3 levels) in getter' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.10/lib/mongoid/threaded/lifecycle.rb:125:in `_loading' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.10/lib/mongoid/relations/accessors.rb:156:in `block (2 levels) in getter' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.10/lib/mongoid/threaded/lifecycle.rb:84:in `_building' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.10/lib/mongoid/relations/accessors.rb:155:in `block in getter' 
    from /opt/cmf/app/models/carrier.rb:120:in `to_email_addresses' 
    from mta_connector.rb:151:in `block in <main>' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.10/lib/mongoid/contextual/mongo.rb:645:in `yield_document' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.10/lib/mongoid/contextual/mongo.rb:134:in `block (2 levels) in each' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.7/lib/moped/query.rb:78:in `block in each' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.7/lib/moped/cursor.rb:26:in `block in each' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.7/lib/moped/cursor.rb:26:in `each' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.7/lib/moped/cursor.rb:26:in `each' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.7/lib/moped/query.rb:77:in `each' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.2.7/lib/moped/query.rb:77:in `each' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.10/lib/mongoid/contextual/mongo.rb:133:in `block in each' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.10/lib/mongoid/contextual/mongo.rb:604:in `selecting' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.10/lib/mongoid/contextual/mongo.rb:132:in `each' 
    from /Users/arigold/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.10/lib/mongoid/contextual.rb:18:in `each' 
    from mta_connector.rb:139:in `<main>' 

回答

0

我在西納特拉不得不明確要求「CARRIER_DOMAIN」腳本。

我以爲它會知道得到的文件,因爲它在rails控制檯中工作(也就是說,我不需要在rails控制檯中)。嗯。

Anyhoo,將require 'carrier_domain'(即未初始化的常量)添加到我的Sinatra腳本中。

相關問題