i want to validate uniqueness of two filed but if second filed is nil just ignore validation i have two model 'Asset' and 'Company' Asset has an unique identifier code what i want to do is to validate uniqueness of identifier code of asset with company. we can check this by
class Asset < ActiveRecord::Base
validates :identifier, :uniqueness => {:scope => :company_id}
end
but this also did not allow nil for two asset
how can i ignore validation of uniqueness of identifier code if its nil
can we pass a block,or add except
or something like that we can do with filters in controller i am looking for some solution like
validates :identifier, :uniqueness => {:scope => :company_id} unless { :identifier.is_nil? }
can i skip validation by some before-validation callback??
Ruby 1.8.7
validates :identifier, :uniqueness => { :scope => :company_id } , :unless => lambda { |asset| !asset.identifier.nil? }
Ruby 1.9.3
validates :identifier, :uniqueness: { scope: :company_id }, unless: lambda { |asset| !asset.identifier.nil? }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With