1

次のようなデータモデルがあります

  • 入札は入札者に関連付けられUserます
  • 入札は単一または単一のいずれかである可能性がofferありlistingますProduct
  • AProductには、複数のユーザーによって投稿された複数のオファーとリスト (別々の) がある場合があります
  • ユーザーはオファーやリストを複数のサイトに掲載できますProducts

商品 <--- 入札 ---> ユーザー

モデルpから既存のものを指定すると、クラスの新しいインスタンスがどこにあるかなどの操作は「ダーティ」としてマークされず、変更はデータベースに永続化されませんProductp.offers << bidbidBidp

製品クラス

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 に答える