模块:Mongoid::Persistable::Updatable

包含在:
Mongoid::Persistable
定义于:
lib/mongoid/persistable/updatable.rb

Overview

定义更新现有文档的持久性操作的行为。

实例方法摘要折叠

实例方法详细信息

# 更新 (attributes = {}) ⇒ true | false也称为: update_attributes

更新数据库中的文档属性。

例子:

更新文档的属性

document.update(:title => "Sir")

参数:

  • 属性 哈希 (默认为: {}

    要更新的属性。

返回:

  • ( true | false )

    如果验证通过,则为 true;如果未通过,则为 false。



40
41
42
43
# File 'lib/mongoid/persistable/updatable.rb', line 40

def update(属性 = {})
  assign_attributes(属性)
  保存
end

# 更新! (attributes = {}) ⇒ true | false也称为: update_attributes!

更新数据库中的文档属性,如果验证失败,则会引发错误。

例子:

更新文档的属性。

document.update!(:title => "Sir")

参数:

  • 属性 哈希 (默认为: {}

    要更新的属性。

返回:

  • ( true | false )

    如果验证通过,则为 true。

引发:

  • ( Errors::Validations )

    如果验证失败。

  • ( Errors::Callbacks )

    如果回调返回 false。



58
59
60
61
62
63
64
65
# File 'lib/mongoid/persistable/updatable.rb', line 58

def update!(属性 = {})
  结果 = update_attributes(属性)
  除非 结果
    fail_due_to_validation! 除非 errors.空?
    fail_due_to_callback!(:update_attributes!)
  end
  结果
end

# update_attribute (name, value) ⇒ true | false

更新单个属性并保留整个文档。 这会跳过验证,但会触发回调。

例子:

更新属性。

person.update_attribute(:title, "Sir")

参数:

  • 名称 (符号 | string )

    属性的名称。

  • ( Object )

    属性的新值。a

返回:

  • ( true | false )

    如果保存成功,则为 true;如果保存失败,则为 false。

引发:



24
25
26
27
28
29
30
# File 'lib/mongoid/persistable/updatable.rb', line 24

def update_attribute(名称, )
  as_writable_attribute!(名称, ) do |访问权限|
    标准化 = 名称.to_s
    process_attribute(标准化, )
    保存(验证: false)
  end
end