1

我有一個數據模型如下嵌入式mongoid文件未標記爲髒/不更新

  • 出價已與放置在出價User相關
  • 出價可以是一個offerlisting在單一Product
  • 一個Product可能有多重優惠和上市(獨立)發佈的由多個用戶
  • 用戶可以將報價和上市多Products

產品< ---投標--->用戶

鑑於現有pProduct模型,操作就像p.offers << bid其中bidBid類的新實例不標記p爲「髒」,並更改不會保存到數據庫

產品類

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

Bid類

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]似乎不工作,要麼

回答

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 
+0

因此,如果出價是用戶擁有的,我應該只使用has_one(出價)和has_man Ÿ(關於用戶)的關係呢? – arcyqwerty

+0

ohh k我正在更新答案,然後.. – abhas

+0

似乎是保存它在兩個offer和列表結束... – arcyqwerty