Mongoid 仅使用 created_at 时间戳

Mongoid use only created_at timestamp

是否可以只为只读文档设置 created_at 时间戳?

我目前有以下消息class

class Message
  include Mongoid::Document
  include Mongoid::Timestamps

  field :text,      type: String

  belongs_to :user, foreign_key: :user_id
  embedded_in :conversation
end

它工作正常,但对于每条消息,我都在浪费 space 与 updated_at 字段,这将始终与 created_at

相同

包括 Mongoid::Timestamps::Created 而不是 Mongoid::Timestamps

class Message
  include Mongoid::Document
  include Mongoid::Timestamps::Created

  field :text,      type: String

  belongs_to :user, foreign_key: :user_id
  embedded_in :conversation
end

浏览 this page 的时间戳部分。

include Mongoid::Timestamps             - created_at and updated_at.
include Mongoid::Timestamps::Created    - created_at only.
include Mongoid::Timestamps::Updated    - updated_at only.

你甚至可以使用简称

include Mongoid::Timestamps::Short           - c_at and u_at.
include Mongoid::Timestamps::Created::Short  - c_at only.
include Mongoid::Timestamps::Updated::Short  - u_at only.