lib/ensurance.rb in ensurance-0.1.23 vs lib/ensurance.rb in ensurance-0.1.24

- old
+ new

@@ -15,11 +15,11 @@ @_ensure_by = [] def ensure_by(*args, order: nil) @_additional_ensure_by = args if (::ActiveRecord::Base.connection rescue false) - @_ensure_order = (order || primary_key).to_s + @_ensure_order = (order || self.primary_key).to_s @_ensure_by = [@_additional_ensure_by || primary_key].flatten.compact.uniq end # ap "Ensure By: #{self}.#{@_ensure_by} Order: #{self}.#{@_ensure_order}" end @@ -34,11 +34,11 @@ return found elsif thing.is_a?(String) && (found = GlobalID::Locator.locate(thing)) return found end - @_ensure_by ||= [@_additional_ensure_by || primary_key].flatten.compact.uniq + @_ensure_by ||= [@_additional_ensure_by || self.primary_key].flatten.compact.uniq @_ensure_order ||= primary_key found = [] things = [thing].flatten things.each do |thing| @@ -58,10 +58,11 @@ nil end else record = find_by(ensure_field => value) if value.present? && !value.is_a?(Hash) end - break if record.is_a?(self) + record = nil if record&.__send__(ensure_field) != value + break if record end found << record end found.compact!