Authlogic, rails3_acts_as_paranoid and validates_uniqueness_of: login + default_scope (: conditions => {: active => true})

I want to be able to create multiple user accounts with the same name (due to the case when the user deletes his account ... and then logs in with the same name). I am using authlogic and rails3_acts_as_paranoid.

But there is a problem: Authlogic checks the uniqueness of the input field - and IGNORES default_scope (: conditions => {: active => true}).

(more details about an invalid error report: https://rails.lighthouseapp.com/projects/8994/tickets/4064-validates_uniqueness_of-should-honor-default_scope-or-not )

I did not learn how to tell validates_uniqueness_of to use the default scope ... Can you help me?

+3
source share
1 answer

Validation

ActiveRecord's built-in uniqueness check does not take into account records deleted by ActsAsParanoid. If you want to check the uniqueness of not only deleted records, use the validates_as_paranoid macro in your model. Then, instead of using validates_uniqueness_of, use validates_uniqueness_of_without_deleted. This will result in deleted records being counted from the uniqueness check.

class Paranoiac < ActiveRecord::Base
  acts_as_paranoid
  validates_as_paranoid
  validates_uniqueness_of_without_deleted :name
end

Paranoiac.create(:name => 'foo').destroy
Paranoiac.new(:name => 'foo').valid? #=> true

luck

+2
source

Source: https://habr.com/ru/post/1770785/


All Articles