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::Basevalidates_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::Basevalidates_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::Basevalidates_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::Basevalidates_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 aWHERE
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 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 atrue
orfalse
value.: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 atrue
orfalse
value.
class Picture < ApplicationRecordbelongs_to :imageable, polymorphic: truevalidates_uniqueness_of :name, scope: [:imageable_id, :imageable_type]
end
ruby on rails validates uniqueness相关推荐
- ruby on rails_如何将刺激添加到Ruby on Rails应用程序
ruby on rails 介绍 (Introduction) If you are working with a Ruby on Rails project, your requirements m ...
- [rails] 我的订餐系统 -- 小试ruby on rails(转)
前言 近期在java社区中一种新的脚本语言ruby,及用ruby开发的一个wab框架 rails也热闹了起来.引起了不少的java开发人员的关注. 本人平时还是很少接触脚本语言方面东东 ...
- ruby on rails_我成为了Ruby on Rails和React的贡献者,你也可以
ruby on rails I am really grateful to have contributed to a few open source projects, including two ...
- 新手安装ruby on rails(ror)的成功必备手册
2019独角兽企业重金招聘Python工程师标准>>> 如何快速正确的安装 Ruby, Rails 运行环境 每一位使用windows系统来进行ROR开发项目的都是这个世界上折翼的天 ...
- 关于 Ruby Ruby on Rails 的一些书及论坛网站
关于 Ruby &Ruby on Rails 的一些书及论坛网站 需要用到的一些书 The Ruby Way Programming Ruby 2nd edition Agile Web ...
- Ruby on Rails的下载及安装以及开发环境的搭建
要基于ruby开发应用程序,我们必须安装ruby.gem.rails.mongrel. 第一,到官方网站上下载最新的Ruby One-Click Installer版本(已经自带了RubyGems,一 ...
- Ruby on Rails路径穿越与任意文件读取漏洞分析(CVE-2019-5418)
Ruby on Rails是一个 Web 应用程序框架,是一个相对较新的 Web 应用程序框架,构建在 Ruby 语言之上.它被宣传为现有企业框架的一个替代,而它的目标,就是让 Web 开发方面的生活 ...
- ruby语言开源Web应用框架 Ruby on Rails 简介
目录 Ruby on Rails是什么 历史 Rails 的 MVC 架构 Web 服务器支持 数据库支持 系统要求 集成开发环境 Ruby on Rails是什么 Ruby on Rails(官方简 ...
- Ruby on rails
转自https://www.cnblogs.com/fantiantian/p/3401913.html Ruby on rails初体验(一) 接触ruby on rails 已经有一段时间了,想记 ...
最新文章
- 作为一个程序员为什么要写博客?
- 网管网络布线之常用兵器谱
- 关于python编码的问题
- SDUT 3379 数据结构实验之查找七:线性之哈希表
- JS快速实现深拷贝--一行就搞定了
- iOS 为tableview添加新的cell类
- 深层神经网络——激活函数去线性化
- linux调整zram大小,ZRAM将在Linux5.1上看到更高的性能-它改变了默认的压缩器
- linux安全技术课程报告,综合实例一+linux平台WEB安全技术研究报告.doc
- 程序员如何开启机器学习之路
- 一文读懂腾讯云“AI即服务”战略新品“智能云”
- 5月书讯 | 哺育小平邦彦、伊藤清等一代数学家的“数学圣经”终于来啦!
- 新路由3 newifi d2 潘多拉PandoraBox固件,带鸡血驱动
- 【参赛作品71】CentOS 7.9 安装 openGauss 2.1.0 之剥茧抽丝
- Firefox数据采集插件大全
- 光驱刻录软件提示需要计算机管理员权限,如何禁用光驱刻录功能、设置光驱只读而禁止刻录的方法...
- [生存志] 第117节 九章算术均徭赋
- C语言 读入中文文本文件txt乱码问题-应该设置为ANSI编码
- Java培训包就业的机构
- 风云三号红外遥感+航天级星基融合定位:埃安星灵架构量产发布
热门文章
- JavaScript获取DOM对象常用方法
- iPhone iOS升级完美指南
- MMDetection亲测安装教程
- 三星同时发展两种电视面板技术,围追堵截LG
- 网站服务器到期后 内容释放,服务器到期后多久释放
- 类的继承关系,多态的体现,我的觉得题目还是有点欠缺
- python中sqrt(4)*sqrt(9)_Python表达式sqrt(4)*sqrt(9)的值为()
- 安卓RatingBar的使用(评分控件)
- 甲骨文服务器一键修改root密码
- 支持多种视频格式的html网页视频播放器代码