ruby on rails validates uniqueness



最近在处理一个小功能,每个元素可以有多个图片,每个图片的name表示了它是背景图还是海报图,

需要对每个元素的图片name做一个唯一性验证,一个元素不能添加两个海报图,


需要使用的是validates_uniqueness_of

http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#method-i-validates_uniqueness_of


Validates whether the value of the specified attributes are unique across the system. Useful for making sure that only one user can be named “davidhh”.

class Person < ActiveRecord::Base
  validates_uniqueness_of :user_name
end

It can also validate whether the value of the specified attributes are unique based on a :scope parameter:

class Person < ActiveRecord::Base
  validates_uniqueness_of :user_name, scope: :account_id
end


Or even multiple scope parameters. For example, making sure that a teacher can only be on the schedule once per semester for a particular class.

class TeacherSchedule < ActiveRecord::Base
  validates_uniqueness_of :teacher_id, scope: [:semester_id, :class_id]
end

It is also possible to limit the uniqueness constraint to a set of records matching certain conditions. In this example archived articles are not being taken into consideration when validating uniqueness of the title attribute:

class Article < ActiveRecord::Base
  validates_uniqueness_of :title, conditions: -> { where.not(status: 'archived') }
end


When the record is created, a check is performed to make sure that no record exists in the database with the given value for the specified attribute (that maps to a column). When the record is updated, the same check is made but disregarding the record itself.

Configuration options:

  • :message - Specifies a custom error message (default is: “has already been taken”).

  • :scope - One or more columns by which to limit the scope of the uniqueness constraint.

  • :conditions - Specify the conditions to be included as a WHERE SQL fragment to limit the uniqueness constraint lookup (e.g. conditions: -> { where(status: 'active') }).

  • :case_sensitive - Looks for an exact match. Ignored by non-text columns (true by default).

  • :allow_nil - If set to true, skips this validation if the attribute is nil (default is false).

  • :allow_blank - If set to true, skips this validation if the attribute is blank (default is false).

  • :if - Specifies a method, proc or string to call to determine if the validation should occur (e.g. if: :allow_validation, or if: Proc.new { |user| user.signup_step > 2 }). The method, proc or string should return or evaluate to a true or false value.

  • :unless - Specifies a method, proc or string to call to determine if the validation should not occur (e.g. unless: :skip_validation, or unless: Proc.new { |user| user.signup_step <= 2 }). The method, proc or string should return or evaluate to a true or false value.



class Picture < ApplicationRecord
  belongs_to :imageable, polymorphic: true

  validates_uniqueness_of :name, scope: [:imageable_id, :imageable_type]
end




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值