我有一个数据模型如下
User
相关联offer
上的出价可能是listing
或Product
Product
可能有多个商家发布的多个优惠和商家信息(单独)Products
产品< --- Bid --->用户
鉴于p
模型中的现有Product
,p.offers << bid
等bid
类Bid
类的新实例之类的操作不标记{{} 1}}为“脏”,并且更改不会持久保存到数据库
产品类
p
出价等级
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!
似乎也不起作用
答案 0 :(得分: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