Do we need another validation context in valid?, i.e. :upsert?.
class Thing
include Mongoid::Document
field :email, type: String
field :name, type: String
validates :email, uniqueness: true
end
t1 = Thing.new(email: 'me@me.com', name: 'Me'); t1.id = 'custom-id'; t1.upsert
t2 = Thing.new(email: 'me@me.com', name: 'You'); t2.id = 'custom-id'; t2.upsert # validation fails even though the upsert would otherwise update the first model and keep the db valid
Suggest we need to be able to:
class Model
validates :foo, uniqueness: true, on: [:create, :update] # but not on :upsert
end
Admittedly it's not ideal as it would allow upsert to be used to put the db in an invalid state:
t3 = Thing.new(email: 'me@me.com', name: 'Me'); t3.id = 'a-different-custom-id'; t3.upsert # gets inserted, breaking the uniqueness constraint
, but it's better than removing the validation entirely I think.