2012-03-06 41 views
1

是否有可能有以下的一种关系:Mongoid - 是否可以嵌入模型的embedded_in?

用户:

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

为什么任务文件中未嵌入两个用户,一个用于发送和一个给用户? – 2012-03-06 16:00:46

+0

添加了一个有你的层次结构的答案,让我知道,如果这是你在找什么。 – 2012-03-06 16:10:05

回答

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 2012-03-07 03:43:51

+0

发件人不能,但'assigne'可以。 – shingara 2012-03-07 08:36:39

+0

当然,根据你的喜好调整。你的例子不起作用的原因是关系的另一方没有定义。在mongoid文档的关系部分查看'inverse_of'。 – 2012-03-08 04:14:53

相关问题