Guest User

Untitled

a guest
Aug 1st, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. Mongoid - Is it possible to embeds_one what the model is embedded_in?
  2. class User
  3. include Mongoid::Document
  4. include Mongoid::Paperclip
  5. include Mongoid::Timestamps
  6.  
  7. store_in :users
  8. belongs_to :team
  9. field :full_name, :type => String
  10. field :email, :type => String
  11.  
  12. attr_accessible :full_name,
  13.  
  14. end
  15.  
  16. class Task
  17. include Mongoid::Document
  18. include Mongoid::Timestamps
  19.  
  20. embedded_in :user
  21. embeds_one :sender, :class_name => "User"
  22.  
  23. field :text, :type => String
  24. field :due_time, :type => DateTime
  25. field :completed, :type => Boolean, :default => false
  26.  
  27. attr_accessible :text, :due_time
  28. end
  29.  
  30. > u1 = User.where(...).first
  31. > u2 = User.where(...).first
  32. > task = Task.new
  33. > task.user = u1
  34. > task.sender = u2
  35.  
  36. NoMethodError: undefined method `first' for #<Task:0x47631bc9>
  37. from org/jruby/RubyKernel.java:227:in `method_missing'
  38. from /Users/larry/.rvm/gems/jruby-1.6.7@entercamp/gems/mongoid-2.4.6/lib/mongoid/attributes.rb:166:in `method_missing'
  39. from /Users/larry/.rvm/gems/jruby-1.6.7@entercamp/gems/mongoid-2.4.6/lib/mongoid/relations/embedded/many.rb:292:in `substitute'
  40. from org/jruby/RubyProc.java:270:in `call'
  41. from org/jruby/RubyProc.java:220:in `call'
  42. from /Users/larry/.rvm/gems/jruby-1.6.7@entercamp/gems/mongoid-2.4.6/lib/mongoid/relations/embedded/atomic.rb:61:in `atomically'
  43. from org/jruby/RubyProc.java:270:in `call'
  44. from org/jruby/RubyProc.java:220:in `call'
  45. 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'
  46. from /Users/larry/.rvm/gems/jruby-1.6.7@entercamp/gems/mongoid-2.4.6/lib/mongoid/relations/embedded/atomic.rb:60:in `atomically'
  47. from /Users/larry/.rvm/gems/jruby-1.6.7@entercamp/gems/mongoid-2.4.6/lib/mongoid/relations/embedded/many.rb:290:in `substitute'
  48. from org/jruby/RubyKernel.java:1787:in `tap'
  49. from /Users/larry/.rvm/gems/jruby-1.6.7@entercamp/gems/mongoid-2.4.6/lib/mongoid/relations/embedded/many.rb:283:in `substitute'
  50. from /Users/larry/.rvm/gems/jruby-1.6.7@entercamp/gems/mongoid-2.4.6/lib/mongoid/relations/accessors.rb:128:in `tasks='
  51. from org/jruby/RubyProc.java:270:in `call'
  52. from org/jruby/RubyKernel.java:2080:in `send'
  53.  
  54. > assignee = User.where(...).first
  55. > assigner = User.where(...).first
  56. > task = Task.new
  57. > task.sender = assigner
  58. > task.save
  59. > assignee.task = task
  60. > assignee.save
  61.  
  62. class Task
  63. embedded_in :assignee, :class_name => "User"
  64. embeds_one :assigner, :class_name => "User"
  65. end
  66.  
  67. class User
  68. embeds_many :tasks
  69. embedded_in :task
  70. end
Add Comment
Please, Sign In to add comment