我有这个关系:
class EmailType
include Mongoid::Document
include Mongoid::Timestamps
has_one :job_status, :inverse_of => :email_type
class JobStatus
include Mongoid::Document
belongs_to :email_type, :inverse_of => :job_status
但是当我尝试创建它们之间的关系时,我得到了一个错误。我的示例遵循与http://two.mongoid.org/docs/relations/referenced/1-1.html上指示的相同模式。
这对我来说是个谜,是错误消息显示NameError: uninitialized constant JobStatu
而不是NameError: uninitialized constant JobStatus
......
Loading development environment (Rails 3.2.12)
2.0.0-p247 :001 > e = EmailType.new
=> #<EmailType _id: 51eafe9183c3366282000001, created_at: nil, updated_at: nil, name: nil, type: nil, value: nil, from: nil, subject: nil, line_break: "\n", order_id_line: nil, order_id_regex: nil, mail_address_line: nil, mail_address_regex: nil, name_line: nil, name_regex: nil, user_id: nil>
2.0.0-p247 :002 > j = JobStatus.new
=> #<JobStatus _id: 51eafea583c3366282000002, group_id: nil, name: nil, status: "new", error_msg: "", extra: {}, email_type_id: nil>
2.0.0-p247 :003 > e.job_status = j
NameError: uninitialized constant JobStatu
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.12/lib/active_support/inflector/methods.rb:230:in `block in constantize'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.12/lib/active_support/inflector/methods.rb:229:in `each'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.12/lib/active_support/inflector/methods.rb:229:in `constantize'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.12/lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/metadata.rb:630:in `klass'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/proxy.rb:54:in `klass'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/referenced/one.rb:21:in `block in initialize'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/proxy.rb:41:in `init'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/referenced/one.rb:20:in `initialize'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/accessors.rb:44:in `new'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/accessors.rb:44:in `create_relation'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/accessors.rb:26:in `__build__'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/accessors.rb:269:in `block (2 levels) in setter'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/accessors.rb:175:in `without_autobuild'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/accessors.rb:263:in `block in setter'
from (irb):3
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.12/lib/rails/commands/console.rb:47:in `start'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.12/lib/rails/commands/console.rb:8:in `start'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.12/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'2.0.0-p247 :004 >
为了确保我没有把它弄混,我试着反过来做:
Loading development environment (Rails 3.2.12)
2.0.0-p247 :001 > e = EmailType.new
=> #<EmailType _id: 51eaff9283c3365840000001, created_at: nil, updated_at: nil, name: nil, type: nil, value: nil, from: nil, subject: nil, line_break: "\n", order_id_line: nil, order_id_regex: nil, mail_address_line: nil, mail_address_regex: nil, name_line: nil, name_regex: nil, user_id: nil>
2.0.0-p247 :002 > j = JobStatus.new
=> #<JobStatus _id: 51eaff9483c3365840000002, group_id: nil, name: nil, status: "new", error_msg: "", extra: {}, email_type_id: nil>
2.0.0-p247 :003 > j.email_type = e
NameError: uninitialized constant JobStatu
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.12/lib/active_support/inflector/methods.rb:230:in `block in constantize'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.12/lib/active_support/inflector/methods.rb:229:in `each'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.12/lib/active_support/inflector/methods.rb:229:in `constantize'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.12/lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/metadata.rb:630:in `klass'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/proxy.rb:54:in `klass'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/referenced/one.rb:21:in `block in initialize'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/proxy.rb:41:in `init'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/referenced/one.rb:20:in `initialize'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/accessors.rb:44:in `new'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/accessors.rb:44:in `create_relation'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/accessors.rb:26:in `__build__'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/accessors.rb:269:in `block (2 levels) in setter'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/accessors.rb:175:in `without_autobuild'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/accessors.rb:263:in `block in setter'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/extensions/object.rb:104:in `do_or_do_not'
... 5 levels...
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/proxy.rb:25:in `bind_one'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/referenced/in.rb:25:in `block in initialize'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/proxy.rb:41:in `init'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/referenced/in.rb:23:in `initialize'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/accessors.rb:44:in `new'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/accessors.rb:44:in `create_relation'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/accessors.rb:26:in `__build__'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/accessors.rb:269:in `block (2 levels) in setter'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/accessors.rb:175:in `without_autobuild'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/mongoid-3.1.3/lib/mongoid/relations/accessors.rb:263:in `block in setter'
from (irb):3
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.12/lib/rails/commands/console.rb:47:in `start'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.12/lib/rails/commands/console.rb:8:in `start'
from /Users/christoffer-jorgensen/.rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.12/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'2.0.0-p247 :004 >