Lynn
Lynn

Reputation: 1123

Rails: validates_uniqueness_of with conditions not working as expected

I have a model that uses an 'active' flag to soft-delete items instead of destroying them. The model has a 'name' property, which must be unique among active items. I was trying to use the conditions modifier with validates_uniqueness_of, but it still seems to be checking for uniqueness across both active and inactive items. What am I doing wrong?

class Foo < ActiveRecord::Base

  attr_accessible :name, :active
  validates_uniqueness_of :name, conditions: -> { where(active:true) }

end

Upvotes: 4

Views: 8738

Answers (4)

Mohsin Mahmood
Mohsin Mahmood

Reputation: 3436

This works for me —

validates_uniqueness_of :asset_id, scope: :source,
                        conditions: -> { where.not(source: :azure) },
                        :allow_nil => true, :allow_blank => true,
                        message: "has already been taken with this source." 

Upvotes: 0

Koen.
Koen.

Reputation: 26949

If the scope differs from the conditions, you can apply the scope within those conditions by adding a conditions key to the uniqueness object:

scope :active, -> { where(active: true) }

validates :active, uniqueness: {
  scope: :name,
  message: 'name is in use',
  conditions: -> { active }
}

Upvotes: 4

user2503775
user2503775

Reputation: 4367

This works for me:

validates :active, :uniqueness => { :scope => :name, :message => 'name is in use'}, :if => :active

Upvotes: 1

Matt
Matt

Reputation: 14038

You can use the scope and if modifiers in conjunction for this:

scope :active, where(:active => true)
validates :name, :uniqueness => {:message => 'That name is in use', :if => :active?, :scope => :active}

This will cause only items that are active to trigger the validation, and the validation will consider uniqueness only among items that are active.

I have confirmed that this works in Rails 3 and 4.

Upvotes: 5

Related Questions