Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
50999974
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
50999974
编写于
5月 16, 2015
作者:
K
Kasper Timm Hansen
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #20173 from imtayadeway/tw/active-record-validations-docs
[ci skip] Improve grammar/syntax in AR validations docs.
上级
796072b8
33513714
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
12 addition
and
12 deletion
+12
-12
guides/source/active_record_validations.md
guides/source/active_record_validations.md
+12
-12
未找到文件。
guides/source/active_record_validations.md
浏览文件 @
50999974
...
...
@@ -47,7 +47,7 @@ built-in helpers for common needs, and allows you to create your own validation
methods as well.
There are several other ways to validate data before it is saved into your
database, including native database constraints, client-side validations
,
database, including native database constraints, client-side validations
and
controller-level validations. Here's a summary of the pros and cons:
*
Database constraints and/or stored procedures make the validation mechanisms
...
...
@@ -122,7 +122,7 @@ database only if the object is valid:
*
`update!`
The bang versions (e.g.
`save!`
) raise an exception if the record is invalid.
The non-bang versions don't
,
`save`
and
`update`
return
`false`
,
The non-bang versions don't
:
`save`
and
`update`
return
`false`
, and
`create`
just returns the object.
### Skipping Validations
...
...
@@ -143,7 +143,7 @@ database regardless of its validity. They should be used with caution.
*
`update_counters`
Note that
`save`
also has the ability to skip validations if passed
`validate:
false`
as argument. This technique should be used with caution.
false`
as a
n a
rgument. This technique should be used with caution.
*
`save(validate: false)`
...
...
@@ -272,7 +272,7 @@ available helpers.
This method validates that a checkbox on the user interface was checked when a
form was submitted. This is typically used when the user needs to agree to your
application's terms of service, confirm
reading some text
, or any similar
application's terms of service, confirm
that some text is read
, or any similar
concept. This validation is very specific to web applications and this
'acceptance' does not need to be recorded anywhere in your database (if you
don't have a field for it, the helper will just create a virtual attribute).
...
...
@@ -338,7 +338,7 @@ In your view template you could use something like
This check is performed only if
`email_confirmation`
is not
`nil`
. To require
confirmation, make sure to add a presence check for the confirmation attribute
(we'll take a look at
`presence`
later on this guide):
(we'll take a look at
`presence`
later on
in
this guide):
```
ruby
class
Person
<
ActiveRecord
::
Base
...
...
@@ -499,9 +499,9 @@ constraints to acceptable values:
default error message for this option is _"must be equal to %{count}"_.
*
`:less_than`
- Specifies the value must be less than the supplied value. The
default error message for this option is _"must be less than %{count}"_.
*
`:less_than_or_equal_to`
- Specifies the value must be less than or equal t
he
supplied value. The default error message for this option is _"must be less
than or equal to %{count}"_.
*
`:less_than_or_equal_to`
- Specifies the value must be less than or equal t
o
the supplied value. The default error message for this option is _"must be
less
than or equal to %{count}"_.
*
`:odd`
- Specifies the value must be an odd number if set to true. The
default error message for this option is _"must be odd"_.
*
`:even`
- Specifies the value must be an even number if set to true. The
...
...
@@ -813,7 +813,7 @@ end
Person
.
new
.
valid?
# => ActiveModel::StrictValidationFailed: Name can't be blank
```
There is also
an ability to pass custom exception to
`:strict`
option.
There is also
the ability to pass a custom exception to the
`:strict`
option.
```
ruby
class
Person
<
ActiveRecord
::
Base
...
...
@@ -877,7 +877,7 @@ end
### Grouping Conditional validations
Sometimes it is useful to have multiple validations use one condition
, i
t can
Sometimes it is useful to have multiple validations use one condition
. I
t can
be easily achieved using
`with_options`
.
```
ruby
...
...
@@ -889,8 +889,8 @@ class User < ActiveRecord::Base
end
```
All validations inside of
`with_options`
block will have automatically passed
the condition
`if: :is_admin?`
All validations inside of
the
`with_options`
block will have automatically
passed
the condition
`if: :is_admin?`
### Combining Validation Conditions
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录