2013-09-05 49 views
3

我有兩個非常簡單的類:與指定CLASS_NAME Mongoid HABTM關係

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選項,但錯誤依然存在。任何輸入讚賞。

我使用: mongoid(4.0.0 38de2e9)與軌道4 /紅寶石2.0.0-P247

回答

3

所以問題是第二has_and_belongs_to_many的名稱。如果你把它改成其他任何東西,它應該可以工作。無論如何,這是一個短的解決方案,你也應該在monogid上提出一個關於這種情況的問題。我很確定這是因爲關係是Mongoid文檔中的內部方法。

+0

剛剛離開我的辦公桌後,我立即感覺到這是唯一的可能性!只是改了名字而已。謝謝阿瑟。 – muichkine