2012-03-06 8 views
1

は関係の以下のようなものを持っていることは可能です:Mongoid - 埋め込み可能なのは何ですか?モデルは埋め込まれていますか?

ユーザー:

class User 
    include Mongoid::Document 
    include Mongoid::Paperclip 
    include Mongoid::Timestamps 

    store_in :users 
    belongs_to :team 
    field :full_name, :type => String 
    field :email,  :type => String 

    attr_accessible :full_name, 

end 

タスク:

class Task 
    include Mongoid::Document 
    include Mongoid::Timestamps 

    embedded_in :user 
    embeds_one :sender, :class_name => "User" 

    field :text,  :type => String 
    field :due_time, :type => DateTime 
    field :completed, :type => Boolean, :default => false 

    attr_accessible :text, :due_time 
end 

を私はrails consoleでそれを試してみたときに、私は次のように得ました:

> u1 = User.where(...).first 
> u2 = User.where(...).first 
> task = Task.new 
> task.user = u1 
> task.sender = u2 

NoMethodError: undefined method `first' for #<Task:0x47631bc9> 
     from org/jruby/RubyKernel.java:227:in `method_missing' 
     from /Users/larry/.rvm/gems/[email protected]/gems/mongoid-2.4.6/lib/mongoid/attributes.rb:166:in `method_missing' 
     from /Users/larry/.rvm/gems/[email protected]/gems/mongoid-2.4.6/lib/mongoid/relations/embedded/many.rb:292:in `substitute' 
     from org/jruby/RubyProc.java:270:in `call' 
     from org/jruby/RubyProc.java:220:in `call' 
     from /Users/larry/.rvm/gems/[email protected]/gems/mongoid-2.4.6/lib/mongoid/relations/embedded/atomic.rb:61:in `atomically' 
     from org/jruby/RubyProc.java:270:in `call' 
     from org/jruby/RubyProc.java:220:in `call' 
     from /Users/larry/.rvm/gems/[email protected]/gems/mongoid-2.4.6/lib/mongoid/relations/embedded/atomic.rb:82:in `count_executions' 
     from /Users/larry/.rvm/gems/[email protected]/gems/mongoid-2.4.6/lib/mongoid/relations/embedded/atomic.rb:60:in `atomically' 
     from /Users/larry/.rvm/gems/[email protected]/gems/mongoid-2.4.6/lib/mongoid/relations/embedded/many.rb:290:in `substitute' 
     from org/jruby/RubyKernel.java:1787:in `tap' 
     from /Users/larry/.rvm/gems/[email protected]/gems/mongoid-2.4.6/lib/mongoid/relations/embedded/many.rb:283:in `substitute' 
     from /Users/larry/.rvm/gems/[email protected]/gems/mongoid-2.4.6/lib/mongoid/relations/accessors.rb:128:in `tasks=' 
     from org/jruby/RubyProc.java:270:in `call' 
     from org/jruby/RubyKernel.java:2080:in `send' 

はこの種の定義です他の何かがうまくいかないのですか?

+0

、送信者用とユーザーのための1つ? –

+0

階層構造を持つ回答を追加しました。探していたものかどうか教えてください。 –

答えて

2

ユーザーの内部に個々のタスクを格納し、そのタスクの内部に送信者ユーザーを格納するように思えます。 、あなたはレールコンソールでこれをテストするために何をしたいのかだろう

た場合:

> assignee = User.where(...).first 
> assigner = User.where(...).first 
> task = Task.new 
> task.sender = assigner 
> task.save 
> assignee.task = task 
> assignee.save 

右?その場合、私はあなたのクラスは、おそらく次のようになりますと思う:

タスク

class Task 
    embedded_in :assignee, :class_name => "User" 
    embeds_one :assigner, :class_name => "User" 
end 

ユーザー・タスク・ドキュメントに二人のユーザーを埋め込むないのはなぜ

class User 
    embeds_many :tasks 
    embedded_in :task 
end 
+0

'embedded_in:task'が' User'モデルに追加されていると、別のモデルの 'embedded_in'のためユーザーは直接アクセスできませんでした。 – larryzhao

+0

送信者は 'assigne' canできません。 – shingara

+0

あなたの好みに合わせて調整してください。あなたの例がうまくいかなかったのは、関係のもう片方が定義されていないからです。モンゴイド文書の関係セクションの 'inverse_of'を見てください。 –

関連する問題