移行ファイルには次のものがあります
def self.up
create_table :payment_agreements do |t|
t.boolean :automatic, :default => true, :null => false
t.string :payment_trigger_on_order
t.references :supplier
t.references :seller
t.references :product
t.timestamps
end
end
product_id が指定されている場合は一意であることを確認したいのですが、null も許可したいので、モデルに次のように記述します。
validates :product_id,
:uniqueness => true,
:allow_nil => true
うまく機能しますが、移行ファイルにインデックスを追加する必要があります
add_index :payment_agreements, :product_id, :unique => true
product_id に 2 つの null 値が挿入されると、明らかに例外がスローされます。移行で単にインデックスを省略することもできますが、次に示すように、同じ product_id を持つ 2 つの PaymentAgreements を取得する可能性があります:同時実行性と整合性
私の質問は、この問題に対処するための最良/最も一般的な方法は何ですか