Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect default scope on uniqueness validation when without_default_scope is used #443

Open
wants to merge 1 commit into
base: core
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/paranoia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def self.acts_as_paranoid(options={})
alias_method :destroy_without_paranoia, :destroy

include Paranoia
class_attribute :paranoia_column, :paranoia_sentinel_value
class_attribute :paranoia_column, :paranoia_sentinel_value, :paranoia_default_scope_enabled

self.paranoia_column = (options[:column] || :deleted_at).to_s
self.paranoia_sentinel_value = options.fetch(:sentinel_value) { Paranoia.default_sentinel_value }
Expand All @@ -248,6 +248,7 @@ def self.paranoia_scope
class << self; alias_method :without_deleted, :paranoia_scope end

unless options[:without_default_scope]
self.paranoia_default_scope_enabled = true
default_scope { paranoia_scope }
end

Expand Down Expand Up @@ -292,7 +293,7 @@ module Validations
module UniquenessParanoiaValidator
def build_relation(klass, *args)
relation = super
return relation unless klass.respond_to?(:paranoia_column)
return relation unless klass.respond_to?(:paranoia_default_scope_enabled) && klass.paranoia_default_scope_enabled
arel_paranoia_scope = klass.arel_table[klass.paranoia_column].eq(klass.paranoia_sentinel_value)
if ActiveRecord::VERSION::STRING >= "5.0"
relation.where(arel_paranoia_scope)
Expand Down