Skip to content

Commit

Permalink
Fix for STI scoping across leafs
Browse files Browse the repository at this point in the history
  • Loading branch information
d-m-u committed Jan 10, 2018
1 parent bb58882 commit d15cf48
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/unique_within_region_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ class UniqueWithinRegionValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return if value.nil?
match_case = options.key?(:match_case) ? options[:match_case] : true
record_base_class = record.class.base_class
field_matches =
if match_case
record.class.where(attribute => value)
record_base_class.where(attribute => value)
else
record.class.where(record.class.arel_attribute(attribute).lower.eq(value.downcase))
record_base_class.where(record_base_class.arel_attribute(attribute).lower.eq(value.downcase))
end
unless field_matches.in_region(record.region_id).where.not(:id => record.id).empty?
record.errors.add(attribute, "is not unique within region #{record.region_id}")
Expand Down

0 comments on commit d15cf48

Please sign in to comment.