2012-03-09 2 views
2

MongoID에 다음과 같은 문제점이 있습니다.
하나씩 많은 두 모델을 만들었습니다.MongoID 및 일대 다 관계 문제

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

하지만 다음과 같은 출력 보았다 : 나는 "has_many"와 "belongs_to와 선을 제거하면

/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>' 

을 그 내가 User 예를 신규 생성 한 후

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 

"그러면 프로그램이 올바르게 작동합니다.
누구든지 내가 뭘 잘못하고 있다고 말할 수 있습니까?

답변

3

Config 클래스와 이미 충돌 할 수있는 것으로 보입니다. 모델 이름을 UserConfig과 같이 변경하십시오.

2

또는 당신은 당신의 클래스 네임 스페이스에 대한 래퍼 모듈을 사용할 수 있습니다

module MyApp 
    class Config 
     ... 
    end 

    class User 
     ... 
    end 
end