如何在 Rails 中使数据库字段只读? [英] How to make a database field read-only in Rails?

查看:31
本文介绍了如何在 Rails 中使数据库字段只读?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含某个字段的数据库表,一旦将其插入到数据库中,就不可能更新.我如何告诉我的模型它不应该允许更新某个字段?

I have a database table with a certain field which should be impossible to update once it has been inserted to the database. How do I tell my model that it shouldn't allow updating of a certain field?

推荐答案

您想使用 attr_readonly:

列为只读的属性将用于创建新记录,但更新操作将忽略这些字段.

Attributes listed as readonly will be used to create a new record but update operations will ignore these fields.

class Customer < ActiveRecord::Base
    attr_readonly :your_field_name
end

这篇关于如何在 Rails 中使数据库字段只读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆