3

我有两个非常简单的类:

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)

4

1 回答 1

3

所以问题是第二个名字has_and_belongs_to_many。如果您将其更改为其他任何内容,它应该可以工作。无论如何,这对您来说是一个简短的解决方案,您还应该在monogid上提交一个关于这种情况的问题。我很确定这种情况正在发生,因为关系是 Mongoid 文档中的一种内部方法。

于 2013-09-05T19:19:27.440 回答