与指定class_name的Mongoid Habtm关系

时间:2013-09-05 16:24:44

标签: mongoid

我有两个非常简单的类:

class Contact
  include Mongoid::Document     
  has_and_belongs_to_many :organizations, :class_name => 'Organization'     
end

class Organization
  include Mongoid::Document
  has_and_belongs_to_many  :relations, :class_name => 'Contact'
end

以下是验证/保存时的错误日志:

>> org = Organization.new
#<Organization _id: 5228ae3c1d41c8678c000001, relation_ids: nil, _type: "Organization">

>> org.valid?
org.valid?
NoMethodError: undefined method `has_key?' for #<Mongoid::Criteria:0x00000008d32bd0>
    from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-38de2e96edc8/lib/mongoid/criteria.rb:501:in `method_missing'
    from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-38de2e96edc8/lib/mongoid/relations/referenced/many.rb:413:in `block in method_missing'
    from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-38de2e96edc8/lib/mongoid/scopable.rb:238:in `with_scope'
    from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-38de2e96edc8/lib/mongoid/relations/referenced/many.rb:412:in `method_missing'
    from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-38de2e96edc8/lib/mongoid/validatable.rb:72:in `read_attribute_for_validation'
    from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validator.rb:151:in `block in validate'
    from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validator.rb:150:in `each'
    from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validator.rb:150:in `validate'
    from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:283:in `_callback_before_145'
    from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:377:in `_run__4324115989737504907__validate__callbacks'
    from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in `run_callbacks'
    from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-38de2e96edc8/lib/mongoid/interceptable.rb:132:in `run_callbacks'
    from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validations.rb:373:in `run_validations!'
    from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validations/callbacks.rb:106:in `block in run_validations!'
    from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:373:in `_run__4324115989737504907__validation__callbacks'
    from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in `run_callbacks'
    from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-38de2e96edc8/lib/mongoid/interceptable.rb:132:in `run_callbacks'
    from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validations/callbacks.rb:106:in `run_validations!'
    from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validations.rb:314:in `valid?'
    from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-38de2e96edc8/lib/mongoid/validatable.rb:98:in `valid?'
    from (irb):3
    from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
    from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
    from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
    from /home/muichkine/src/white/bin/rails:4:in `require'
    from /home/muichkine/src/white/bin/rails:4:in `<top (required)>'
    from -e:1:in `load'
    from -e:1:in `<main>'
>> contact = Contact.new
contact = Contact.new
#<Contact _id: 5228ae441d41c8678c000002, organization_ids: nil, _type: "Contact">

>> contact.valid?
true

如您所见,在验证组织时验证联系人是否有效。现在,我确实尝试在双方添加:inverse_of选项,但错误仍然存​​在。任何意见都表示赞赏。

我用:     使用Rails 4 / Ruby 2.0.0-p247的mongoid(4.0.0 38de2e9)

1 个答案:

答案 0 :(得分:3)

所以问题是第二个has_and_belongs_to_many的名称。如果你把它改成其他任何东西它应该工作。无论如何,对你来说这是一个简短的解决方案,你也应该在monogid上就这种情况提出问题。我非常确定这种情况正在发生,因为关系是Mongoid文档的内部方法。