1

我发现了另一个类似的问题,并认为我正在遵循此处引用的指示,但仍然遇到此错误。

根据我在我的红宝石脚本中调用的说明Mongoid.load!("path/to/your/mongoid.yml"),所以我不认为是这样。

也许,根据来自的错误消息inflector,这是我如何定义 CarrierDomain 的问题。我正在引用 Sinatra 脚本中的 rails 模型。它位于一个名为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>'
4

1 回答 1

0

我必须在我的 Sinatra 脚本中明确要求“carrier_domain”。

我原以为它会知道获取文件,因为它在 rails 控制台中工作(也就是说,我不需要在 rails 控制台中要求它)。唔。

Anyhoo,将require 'carrier_domain'(即未初始化的常量)添加到我的 Sinatra 脚本中有效。

于 2012-12-05T00:40:52.153 回答