关闭“updated_at” Rails中的列 [英] Turn off "updated_at" column in Rails
问题描述
我有一个简单的Log模型,记录调用控制器操作的事实。
I have a simple "Log" model, that records the fact of calling controller's action.
这个log记录的条目应该创建一次,从未改变。此外,我将在数据库中有许多这些记录。
Entries of this "log" record are supposed to be created once and never altered. Also, I will have many of these records in the database.
因此,没有必要使用updated_at列(不需要浪费硬盘上的内存)。
So, there is no need for "updated_at" column (don't need to waste the memory on HDD).
如何告诉Rails只留下created_at列而不使用updated_at?
How can I tell to Rails to leave only "created_at" column and not to use "updated_at"?
有没有办法使Log模型只读?
Is there any way to make the "Log" model read only?
推荐答案
readonly?
方法。
class Log < ActiveRecord::Base
# Prevent modification of existing records
def readonly?
!new_record?
end
# Prevent objects from being destroyed
def before_destroy
raise ActiveRecord::ReadOnlyRecord
end
end
上面的示例是从此处。
如果您不需要 updated_at
列,只需从数据库中删除(或不添加)它。 Rails不会更新没有的内容。
If you don't need the updated_at
column, just remove (or don't add it) it from your database. Rails won't update what's not there.
这篇关于关闭“updated_at” Rails中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!