Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoid use only created_at timestamp

Is there anyway to set only the created_at timestamp for read-only documents?

I currently have the following message class

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

  field :text,      type: String

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

It works ok, but for every message I'm wasting space with the updated_at field, which will always be the same as created_at

like image 639
Juan Fuentes Avatar asked Oct 17 '17 15:10

Juan Fuentes


2 Answers

Go through Timestamping section of this page.

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

You can even have short names

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.
like image 118
krishnar Avatar answered Nov 10 '22 07:11

krishnar


Include Mongoid::Timestamps::Created instead of 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
like image 2
max Avatar answered Nov 10 '22 06:11

max