1

我有一个数据模型如下

  • 出价与出价相关User
  • 出价可以是一个offerlisting一个Product
  • AProduct可能有多个用户发布的多个报价和列表(单独)
  • 用户可以将优惠和列表放置在多个Products

产品 <--- 投标 ---> 用户

p给定模型中的现有值Product,诸如此类的新实例在p.offers << bid哪里之类的操作不会标记为“脏”,并且更改不会持久保存到数据库中bidBidp

产品类别

class Product
  include Mongoid::Document
  ...
  embeds_many :offers, class_name: 'Bid'
  embeds_many :listings, class_name: 'Bid'
end

投标等级

class Bid
  include Mongoid::Document
  belongs_to :user
  belongs_to :product

  field :amount, type: Money  
  field :timestamp, type: DateTime, default: ->{ Time.now }
end

此外,调用bid.save!或创建新数组p.offers = Array.new [bid]似乎也不起作用

4

1 回答 1

1

更新:

你的模型结构应该是

class Product
   include Mongoid::Document
   ...
   has_many :offers, class_name: 'Bid', :inverse_of => :offers_bid
   has_many :listings, class_name: 'Bid', :inverse_of => :listings_bid
end

class Bid
   include Mongoid::Document
   belongs_to :offers_bid, :class_name => 'Product', :inverse_of => :offers
   belongs_to :listings_bid, :class_name => 'Product', :inverse_of => :listings
   belongs_to :user

   field :amount, type: Money  
   field :timestamp, type: DateTime, default: ->{ Time.now }
end
于 2012-09-07T06:50:42.410 回答