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 aWHERESQL 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 (trueby default).:allow_nil- If set totrue, skips this validation if the attribute isnil(default isfalse).:allow_blank- If set totrue, skips this validation if the attribute is blank (default isfalse).:if- Specifies a method, proc or string to call to determine if the validation should occur (e.g.if: :allow_validation, orif: Proc.new { |user| user.signup_step > 2 }). The method, proc or string should return or evaluate to atrueorfalsevalue.:unless- Specifies a method, proc or string to call to determine if the validation should not occur (e.g.unless: :skip_validation, orunless: Proc.new { |user| user.signup_step <= 2 }). The method, proc or string should return or evaluate to atrueorfalsevalue.
class Picture < ApplicationRecord
belongs_to :imageable, polymorphic: true validates_uniqueness_of :name, scope: [:imageable_id, :imageable_type]
end
ruby on rails validates uniqueness的更多相关文章
- ruby on rails validates
validates :conclusion, :presence => true, :inclusion => { :in => [0,1] } validates :email, ...
- Ruby on Rails Tutorial读书笔记-1
只是怕忘了命令,全部撸一次,记个大概.. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 安装Ruby之前,先要安装RVM: curl -L https://get.rvm.io | bas ...
- Ruby on Rails 开发笔记
安装 Ruby on Rails Install Rails: A step-by-step guide 创建应用 # 创建新的应用程序 $ rails new blog $ cd blog # 启动 ...
- 开发新手最容易犯的50个 Ruby on Rails 错误(1)
[编者按]本文最早发布与 JETRuby 博客,主要介绍了开发新手最容易犯的 Ruby 错误.文章系国内 ITOM 管理平台 OneAPM 编译呈现. 一年前,我们创立了以 "Rubyboo ...
- Linux超快速安装Ruby on Rails
Linux超快速安装Ruby on Rails 时间 2014-11-25 11:45:11 Flincllck Talk 原文 http://www.flincllck.com/quick-ins ...
- 管理不同版本ruby和rails的利器——rvm
近年来,ruby on rails逐渐火了起来,我想各位码农早就耳闻,特别是那些做B/S项目的童鞋,早就想跃跃一试了. 笔者也是初次接触ruby on rails ,我想,对于初学者来说,最好的学习方 ...
- ruby on rails on windows
这次想系统学会rails,最终目标是将redmine改造成顺手的工具,主要的手段就是开发redmine插件.虽然网上都推荐使用类Unix系统,可手头只有win7系统,就安装了. 难免会遇到这样那样的问 ...
- win8平台下Ruby on Rails的第一个web应用
最近在做一个网站web前端的前期开发,老板要求用Ruby on Rails搭建部署开发环境,上网搜之,发现整个搭建流程比较坑爹,于是用了一款集成软件Bitnami Ruby Stack一键安装到我的w ...
- 为什么学习Ruby On Rails:
简单总结了一下自己为什么喜欢ruby on rails: 语法简单,写代码很愉快,比较接近伪代码: 喜欢其强大的正则表达式和字符串操作. ruby中面向对象更自由,更动态: ruby给人信任,相信你了 ...
随机推荐
- SSH原理与运用(一):远程登录
SSH是每一台Linux电脑的标准配置. 随着Linux设备从电脑逐渐扩展到手机.外设和家用电器,SSH的使用范围也越来越广.不仅程序员离不开它,很多普通用户也每天使用. SSH具备多种功能,可以用于 ...
- ubuntu下Eclipse下添加GBK编码
把Windows下的工程导入到了Linux下Eclipse中,由于以前的工程代码,都是GBK编码的(Windows下的Eclipse 默认会去读取系统的编码,所以Widnwos下的Eclipse的编码 ...
- JavaScript学习笔记——变量和数据类型
一.javascript命名规范 1. 严格区分大小写 2. 变量的命名必须以字母或 _或 $开头,余下的部分可以是任意的字母,数字,或者是 _或者是$ 3.不能用关键字或者是保留字命名. 4.jav ...
- hasClass addClass removeClass
//函数有class function hasClass(ele,cls){ return -1<(" "+ele.className+" ").inde ...
- Linux版MonoDevelop无法连接调试器的解决方案(Could not connet to the debugger)
安装了Linux版本的MonoDevelop之后,在运行程序的时候会提示Could not connnet to the debugger.的错误. 原因是新版本的Gnome Terminal不再接受 ...
- GLSL扩展预处理器(支持#include)
http://www.gamedev.net/topic/645610-do-you-extend-glsl/ https://github.com/ehsan/ogre/blob/master/Re ...
- init.php 建立自己的前端共享文件
文件位置:include/init.php 1.新建文件lib_xxx.php(lib_liangxin.php) 2.在文件init.php 第74行加入代码 require(ROOT_PATH . ...
- js获取锚点名称 #
var thisId = window.location.hash; alert(thisId); 输出 #2
- 查找“CDN、负载均衡、反向代理”等大型网络真实IP地址的方法
首先,CDN.负载均衡.反向代理还分为很多层,有时查出来的是最外层的 CDN 服务器群,真实的机器是不对外开放的,类似这样的: 用户 → CDN 网络 → 一台或多台真实机器 ↗ CDN Server ...
- jQuery监听键盘事件及相关操作使用教程
一.首先需要知道的是: 1.keydown() keydown事件会在键盘按下时触发. 2.keyup() keyup事件会在按键释放时触发,也就是你按下键盘起来后的事件 3.keypress() k ...