最近在处理一个小功能,每个元素可以有多个图片,每个图片的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

ruby on rails validates uniqueness的更多相关文章

  1. ruby on rails validates

    validates :conclusion, :presence => true, :inclusion => { :in => [0,1] } validates :email, ...

  2. Ruby on Rails Tutorial读书笔记-1

    只是怕忘了命令,全部撸一次,记个大概.. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 安装Ruby之前,先要安装RVM: curl -L https://get.rvm.io | bas ...

  3. Ruby on Rails 开发笔记

    安装 Ruby on Rails Install Rails: A step-by-step guide 创建应用 # 创建新的应用程序 $ rails new blog $ cd blog # 启动 ...

  4. 开发新手最容易犯的50个 Ruby on Rails 错误(1)

    [编者按]本文最早发布与 JETRuby 博客,主要介绍了开发新手最容易犯的 Ruby 错误.文章系国内 ITOM 管理平台 OneAPM 编译呈现. 一年前,我们创立了以 "Rubyboo ...

  5. Linux超快速安装Ruby on Rails

    Linux超快速安装Ruby on Rails 时间 2014-11-25 11:45:11 Flincllck Talk 原文  http://www.flincllck.com/quick-ins ...

  6. 管理不同版本ruby和rails的利器——rvm

    近年来,ruby on rails逐渐火了起来,我想各位码农早就耳闻,特别是那些做B/S项目的童鞋,早就想跃跃一试了. 笔者也是初次接触ruby on rails ,我想,对于初学者来说,最好的学习方 ...

  7. ruby on rails on windows

    这次想系统学会rails,最终目标是将redmine改造成顺手的工具,主要的手段就是开发redmine插件.虽然网上都推荐使用类Unix系统,可手头只有win7系统,就安装了. 难免会遇到这样那样的问 ...

  8. win8平台下Ruby on Rails的第一个web应用

    最近在做一个网站web前端的前期开发,老板要求用Ruby on Rails搭建部署开发环境,上网搜之,发现整个搭建流程比较坑爹,于是用了一款集成软件Bitnami Ruby Stack一键安装到我的w ...

  9. 为什么学习Ruby On Rails:

    简单总结了一下自己为什么喜欢ruby on rails: 语法简单,写代码很愉快,比较接近伪代码: 喜欢其强大的正则表达式和字符串操作. ruby中面向对象更自由,更动态: ruby给人信任,相信你了 ...

随机推荐

  1. spark操作geoip的domain数据库

    val ipv4 = sc.textFile("hdfs://hbase11:9000/sparkTsData/GeoIP2-Domain-Blocks-IPv4.csv").ma ...

  2. EnableViewState

    EnableViewState 系统默认的值为true,在传递状态值时就包括该控件: 为false,则传递状态值时则不包括它. 可以提高网络访问的速度. 某些控件是不需要接受用户的操作或只需要接受一次 ...

  3. C#验证子网掩码的正确性

    1.       IP合法关于IP地址的合法性验证很简单,方法也很多,比如字符串分解.正则表达式等. 2.       子网掩码是否合法简单来讲,子网掩码就类似这样一串数字,前面一段是连续的1, 类似 ...

  4. MySQL学习笔记——安装及配置环境

    1.安装的版本为mysql-5.6.24-win32.1432006610压缩版 查看教程http://jingyan.baidu.com/article/f3ad7d0ffc061a09c3345b ...

  5. 在线考试系统(Online Exam System)--ASP.NET

    用户设计 -|学生 -|老师 -|管理员 学生结构设计 -|个人信息管理 -|修改个人信息 -|修改登录密码 -|选课中心 -|显示所有老师所开课的信息可进行选课 -|显示自己已选课程 -|在线考试 ...

  6. Python开发【第六篇】:模块

    模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...

  7. PuzzleGame部分核心算法

    #include   "mainwindow.h" #include   <QGridLayout> #include   <QPushButton> #i ...

  8. Web 安全测试

    http://blog.sina.com.cn/s/blog_a1bbddc70101dt12.html http://blog.csdn.net/pdn2000/article/details/64 ...

  9. Mono资源

    摘要 最近看了一部分mono方面的资料,这里整理一下,在这里列一个目录,方便以后用到的时候查找. Mono Mono 是一个由 Xamarin 公司(先前是 Novell,最早为 Ximian)所主持 ...

  10. Linux平台延时之sleep、usleep、nanosleep、select比较

    Linux平台延时之sleep.usleep.nanosleep.select比较 标签: 嵌入式thread线程cpu多线程 2015-05-05 15:28 369人阅读 评论(0) 收藏 举报 ...