嵌入式mongoid文档未标记为脏/未更新

时间:2012-09-07 05:42:51

标签: ruby-on-rails ruby-on-rails-3 activerecord mongoid activemodel

我有一个数据模型如下

  • 出价与出价的User相关联
  • 单个offer上的出价可能是listingProduct
  • Product可能有多个商家发布的多个优惠和商家信息(单独)
  • 用户可以在多个Products
  • 上放置优惠和列表

产品< --- Bid --->用户

鉴于p模型中的现有Productp.offers << bidbidBid类的新实例之类的操作不标记{{} 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!似乎也不起作用

1 个答案:

答案 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