2012-03-09 94 views
2

我遇到了以下MongoID問題。
我創建了兩個相關的模型。MongoID和一對多關係的問題

require 'mongoid' 

class User 
    include Mongoid::Document 
    has_many :configs 

    field :login, :type => String, unique: true 
    field :password, :type => String 
    field :email, :type => String 
end 

class Config 
    include Mongoid::Document 
    belongs_to :user 

    field :links, :type => Array 
    field :root, :type => Array 
    field :objects, :type => Array 
    field :categories, :type => Array 
end 

之後,我已經創造了新的User例如:

user = User.new 
user.login = "login1" 
user.password = "password1" 
user.email = "email1" 
user.save 

但是我看到下面的輸出:

/home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/inflector/methods.rb:229: Use RbConfig instead of obsolete and deprecated Config. 
/home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/relations/referenced/many.rb:550:in `criteria': undefined method `where' for RbConfig:Module (NoMethodError) 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/relations/metadata.rb:143:in `criteria' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/relations/builders/referenced/many.rb:20:in `build' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/relations/accessors.rb:43:in `create_relation' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/relations/accessors.rb:26:in `build' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/relations/accessors.rb:101:in `block (4 levels) in getter' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/threaded/lifecycle.rb:125:in `_loading' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/relations/accessors.rb:100:in `block (3 levels) in getter' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/threaded/lifecycle.rb:84:in `_building' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/relations/accessors.rb:99:in `block (2 levels) in getter' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/validations.rb:51:in `read_attribute_for_validation' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activemodel-3.2.2/lib/active_model/validator.rb:151:in `block in validate' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activemodel-3.2.2/lib/active_model/validator.rb:150:in `each' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activemodel-3.2.2/lib/active_model/validator.rb:150:in `validate' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:310:in `_callback_before_1' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:407:in `_run__852778899__validate__1039643020__callbacks' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:405:in `__run_callback' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:385:in `_run_validate_callbacks' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:81:in `run_callbacks' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/callbacks.rb:43:in `run_callbacks' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activemodel-3.2.2/lib/active_model/validations.rb:212:in `run_validations!' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activemodel-3.2.2/lib/active_model/validations/callbacks.rb:53:in `block in run_validations!' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:403:in `_run__852778899__validation__1039643020__callbacks' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:405:in `__run_callback' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:385:in `_run_validation_callbacks' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:81:in `run_callbacks' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/callbacks.rb:43:in `run_callbacks' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activemodel-3.2.2/lib/active_model/validations/callbacks.rb:53:in `run_validations!' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activemodel-3.2.2/lib/active_model/validations.rb:179:in `valid?' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/validations.rb:75:in `valid?' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/activemodel-3.2.2/lib/active_model/validations.rb:187:in `invalid?' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/persistence/insertion.rb:23:in `block in prepare' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/persistence/insertion.rb:22:in `tap' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/persistence/insertion.rb:22:in `prepare' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/persistence/operations/insert.rb:26:in `persist' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/persistence.rb:49:in `insert' 
    from /home/scepion1d/Workspace/RubyMine/dana-x/.bundle/ruby/1.9.1/gems/mongoid-2.4.6/lib/mongoid/persistence.rb:154:in `upsert' 
    from parser/parsing_starter.rb:47:in `<main>' 

如果我刪除了 「的has_many」 和「belongs_to的行「那麼程序將正常工作。
任何人都可以告訴我做錯了什麼?

回答

3

看起來像是和你的Config類有衝突,而且可能已經被定義了。嘗試將模型的名稱更改爲UserConfig之類的內容。

2

或者你可以使用一個包裝模塊命名空間的類:

module MyApp 
    class Config 
     ... 
    end 

    class User 
     ... 
    end 
end