lib/sequel/plugins/validation_helpers.rb in sequel-4.6.0 vs lib/sequel/plugins/validation_helpers.rb in sequel-4.7.0
- old
+ new
@@ -200,10 +200,12 @@
#
# This validation does not respect the :allow_* options that the other validations accept,
# since it can deal with a grouping of multiple attributes.
#
# Possible Options:
+ # :dataset :: The base dataset to use for the unique query, defaults to the
+ # model's dataset.
# :message :: The message to use (default: 'is already taken')
# :only_if_modified :: Only check the uniqueness if the object is new or
# one of the columns has been modified.
# :where :: A callable object where call takes three arguments, a dataset,
# the current object, and an array of columns, and should return
@@ -229,15 +231,16 @@
where = opts[:where]
atts.each do |a|
arr = Array(a)
next if arr.any?{|x| errors.on(x)}
next if opts[:only_if_modified] && !new? && !arr.any?{|x| changed_columns.include?(x)}
+ ds = opts[:dataset] || model.dataset
ds = if where
- where.call(model.dataset, self, arr)
+ where.call(ds, self, arr)
else
vals = arr.map{|x| send(x)}
next if vals.any?{|v| v.nil?}
- model.where(arr.zip(vals))
+ ds.where(arr.zip(vals))
end
ds = yield(ds) if block_given?
ds = ds.exclude(pk_hash) unless new?
errors.add(a, message) unless ds.count == 0
end