1

Is it possible to have the following kind of relationship:

User:

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

Task:

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

but when I try it out in the rails console, I got the following:

>  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/jruby-1.6.7@entercamp/gems/mongoid-2.4.6/lib/mongoid/attributes.rb:166:in `method_missing'
        from /Users/larry/.rvm/gems/jruby-1.6.7@entercamp/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/jruby-1.6.7@entercamp/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/jruby-1.6.7@entercamp/gems/mongoid-2.4.6/lib/mongoid/relations/embedded/atomic.rb:82:in `count_executions'
        from /Users/larry/.rvm/gems/jruby-1.6.7@entercamp/gems/mongoid-2.4.6/lib/mongoid/relations/embedded/atomic.rb:60:in `atomically'
        from /Users/larry/.rvm/gems/jruby-1.6.7@entercamp/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/jruby-1.6.7@entercamp/gems/mongoid-2.4.6/lib/mongoid/relations/embedded/many.rb:283:in `substitute'
        from /Users/larry/.rvm/gems/jruby-1.6.7@entercamp/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'

is this kind of definition is not possible or there's something else going wrong?

larryzhao
  • 3,173
  • 2
  • 40
  • 62

1 Answers1

2

It sounds like you want to store individual Tasks inside of Users and the sender User inside of that task.

In which case, what you want to do to test this in the rails console would be:

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

Right? In that case I think your classes would probably look like this:

Task

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

User

class User
  embeds_many :tasks
  embedded_in :task
end
Tyler Brock
  • 29,626
  • 15
  • 79
  • 79
  • if `embedded_in :task` is added to `User` model, then user could not be accessed directly because it's `embedded_in` another model. – larryzhao Mar 07 '12 at 03:43
  • The sender can't but the `assigne` can. – shingara Mar 07 '12 at 08:36
  • Sure, tweak to your liking. The reason your example didn't work is that the other side of the relation wasn't defined. Look into 'inverse_of' in the relations section of the mongoid documentation. – Tyler Brock Mar 08 '12 at 04:14