简体   繁体   English

Ruby Rails 语法说明

[英]Ruby Rails syntax clarification

class Person < ApplicationRecord
  validates :name, uniqueness: { case_sensitive: false }
end

When a model has above definition, what exactly is happening behind the scenes?当模型具有上述定义时,幕后究竟发生了什么?

My guess is there exists some validates method and a parameter is passed with symbol name.我的猜测是存在一些validates方法,并且使用符号名称传递参数。 What is second parameter?什么是第二个参数? a hash with a value that is hash?具有哈希值的哈希?

First validation :name lets know that Person is not valid without a name attribute.第一次验证:name让我们知道Person在没有name属性的情况下无效。

Second validation uniqueness二次验证uniqueness

This helper validates that the attribute's value is unique right before the object gets saved.这个助手在对象被保存之前验证属性的值是唯一的 It does not create a uniqueness constraint in the database, so it may happen that two different database connections create two records with the same value for a column that you intend to be unique.它不会在数据库中创建唯一性约束,因此可能会发生两个不同的数据库连接为您打算唯一的列创建两个具有相同值的记录。 To avoid that, you must create a unique index on that column in your database.为避免这种情况,您必须在数据库中的该列上创建唯一索引。

Third { case_sensitive: false }第三个{ case_sensitive: false }

There is also a case_sensitive option that you can use to define whether the uniqueness constraint will be case sensitive or not.还有一个case_sensitive选项可用于定义唯一性约束是否区分大小写。 This option defaults to true此选项默认为 true

Finally validates :name, uniqueness: { case_sensitive: false }最后验证 :name, uniqueness: { case_sensitive: false }

It means in Person model name attribute must be present with uniqueness not be case sensitive.这意味着在 Person 模型名称属性中必须存在唯一性,不区分大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM