0

1º用户约翰有很多礼物

用户.rb

class User
  include Mongoid::Document
  has_many :gifts, dependent: :destroy, :autosave => true
  has_many :orders, dependent: :destroy, :autosave => true
 end

礼物.rb

 class Gift
      include Mongoid::Document
      belongs_to :user
      has_many :orders ,dependent: :destroy, :autosave => true
     end

2º 用户Anthony给John买礼物并下新订单

class Order
   include Mongoid::Document
   belongs_to :gift
   belongs_to :user
  end

现在,用户 Anthony 想要访问他所做的所有销售。

这里的挑战是用户可能有两个角色,买方或卖方。

我应该如何在 Antonio 可以访问其销售额的模型之间建立关系?

4

1 回答 1

2

模型结构

class User
  include Mongoid::Document

  has_many :gifts, dependent: :destroy, :autosave => true
  has_many :orders, dependent: :destroy, :autosave => true
end

class Gift
  include Mongoid::Document

  belongs_to :user
  belongs_to :gifted_to, :class_name => 'User'

  has_one :order ,dependent: :destroy, :autosave => true
end

class Order
  include Mongoid::Document

  belongs_to :gift
  # below associtation is just for quicker ref 
  # otherwise you can have access to it via gift also
  belongs_to :user
end
于 2012-08-14T11:19:56.780 回答