Reputation: 1325
I have this relation:
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
But when I try to create the relation between them I get an error. My example follows as far as I can see the same pattern as instructed on http://two.mongoid.org/docs/relations/referenced/1-1.html.
What makes this a mystery for me is that the error message displays NameError: uninitialized constant JobStatu
and not 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 >
Just to be sure that I haven't mixed it up I have tried to do it the other way round:
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 >
Upvotes: 0
Views: 1525
Reputation: 947
Shorter solution:
has_one :job_status, :inverse_of => :email_type, :class_name => "JobStatus"
UPDATE:
@Ophion: I meant this should work:
class EmailType
include Mongoid::Document
include Mongoid::Timestamps
has_one :job_status, :inverse_of => :email_type, :class_name => "JobStatus"
Explicitly add the class_name to the relationship so Mongoid
will use it directly without having to singularize to get the Model name.
Upvotes: 0
Reputation: 412
You're using ruby 2.0, and I don't have anything to check it with, but to help you, try a simpler name, likely something without an S
in the end. Statu
instead of Status
means that mongoid tries to make it singluar, when searching for an specific model.
Your model sounds alright and you should be able to do it in reverse, so try e.job_status = j
, and see if it truly works.
Upvotes: 2
Reputation: 3036
It is a well-known Mongoid problem, described here in the official documentation
One possible workaround would be doing as suggested in the aforementioned document:
ActiveSupport::Inflector.inflections do |inflect|
inflect.singular("jobstatus", "jobstatus")
end
Or something close to that.
Upvotes: 4