如何确保在Rails 3 Active Record中只更新一次属性?

时间:2017-04-24 06:37:36

标签: ruby ruby-on-rails-3 activerecord

想知道什么是处理我需要更新特定列的情况的最佳方法,一旦更新它不应该允许用户再次更新。

我尝试使用 已更改? 方法检查已更改但尚未保存的记录。但是这不会检查该行中的特定属性。

有什么建议吗?

提前致谢。

2 个答案:

答案 0 :(得分:2)

{
            "apiVersion": "[parameters('eventHubVersion')]",
            "name": "[variables('eventHubNamespaceName')]",
            "type": "Microsoft.EventHub/namespaces",
            "location": "[resourceGroup().location]",
            "resources": [
                {
                    "apiVersion": "2014-09-01",
                    "name": "[variables('eventHubName')]",
                    "type": "eventHubs",
                    "dependsOn": [
                        "[concat('Microsoft.EventHub/namespaces/', variables('eventHubNamespaceName'))]"
                    ],
                    "properties": {
                        "path": "[variables('eventHubName')]",
                        "MessageRetentionInDays": "[parameters('messageRetentionInDays')]",
                        "PartitionCount": "[parameters('partitionCount')]"
                    },
                    "resources": [
                        {
                            "apiVersion": "[parameters('eventHubVersion')]",
                            "name": "StorageRetention",
                            "type": "consumergroups",
                            "dependsOn": [
                                "[variables('eventHubName')]",
                                "[concat('Microsoft.EventHub/namespaces/', variables('eventHubNamespaceName'))]"
                            ],
                            "tags": {
                                "displayName": "eh"
                            }
                        }
                    ]
                },
                {
                    "apiVersion": "[parameters('eventHubVersion')]",
                    "name": "[concat(variables('eventHubNamespaceName'),'/SendOnlyKey')]",
                    "type": "Microsoft.EventHub/namespaces/authorizationRules",
                    "dependsOn": [
                        "[concat('Microsoft.EventHub/namespaces/', variables('eventHubNamespaceName'))]"
                    ],
                    "location": "[resourceGroup().location]",
                    "properties": {
                        "KeyName": "SendOnlyKey",
                        "ClaimType": "SendSharedAccessKey",
                        "ClaimValue": "None",
                        "PrimaryKey": "[parameters('eventHubSendPrimaryKey')]",
                        "SecondaryKey": "your_key",
                        "Rights": [ "Send" ],
                        "Revision": -1
                    }
                },
                {
                    "apiVersion": "[parameters('eventHubVersion')]",
                    "name": "[concat(variables('eventHubNamespaceName'),'/ListenOnlyKey')]",
                    "type": "Microsoft.EventHub/namespaces/authorizationRules",
                    "dependsOn": [
                        "[concat('Microsoft.EventHub/namespaces/', variables('eventHubNamespaceName'))]"
                    ],
                    "location": "[resourceGroup().location]",
                    "properties": {
                        "KeyName": "ListenOnlyKey",
                        "ClaimType": "ReceiveSharedAccessKey",
                        "ClaimValue": "None",
                        "PrimaryKey": "your_key",
                        "SecondaryKey": "your_key",
                        "Rights": [ "Listen" ],
                        "Revision": -1
                    }
                }
            ]
        }

在生成的文件中设置

rails g migration add_value_changed_to_YOUR_TABLE_NAME value_changed:boolean

并运行迁移。

现在,一旦值发生变化,请将此default: false 更新为value_changed

true

答案 1 :(得分:0)

假设您有用户类:

class User < ActiveRecord::Base
  before_save :check_name_not_changed

  private
  def check_name_not_changed
    if self.name.present? && self.name_changed?
      errors.add(:name, "can not be updated")
    end
  end
end

我假设name属性是之前的设置。