次のようなデータモデルがあります
- 入札は入札者に関連付けられ
User
ます - 入札は単一または単一のいずれかである可能性が
offer
ありlisting
ますProduct
- A
Product
には、複数のユーザーによって投稿された複数のオファーとリスト (別々の) がある場合があります - ユーザーはオファーやリストを複数のサイトに掲載できます
Products
商品 <--- 入札 ---> ユーザー
モデルp
から既存のものを指定すると、クラスの新しいインスタンスがどこにあるかなどの操作は「ダーティ」としてマークされず、変更はデータベースに永続化されませんProduct
p.offers << bid
bid
Bid
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!
さらに、新しい配列の呼び出しまたは作成p.offers = Array.new [bid]
も機能しないようです