Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TimeStamps in Mongoid Embedded Documents

I am having a collection A which embeds collection B. Collection A as well as collection B includes Mongoid Timestamps (created_at and updated_at).

Now when I create a new entry of collection B (embedded object) using Rails admin, time stamps saved in Database are nil. But if I create a entry from rails console or from a normal api, then timestamps saved in database are not nil.

Any help would be appreciated.

EDIT:

class B
  include Mongoid::Document
  include Mongoid::Timestamps::Created
  include Mongoid::Timestamps::Updated

  field :user_id,    type: String
  field :message,    type: String
  field :status,     type: Integer, default: 1
  field :spam_count, type: Integer, default: 0

  embedded_in :A

Class B is embedded in class A. When a entry of B is created inside A through rails admin, then created_at and updated_at fields of B are getting saved as nil.

like image 855
Neeraj Yadav Avatar asked Dec 11 '22 17:12

Neeraj Yadav


1 Answers

Without class A or information what mongoid version you use (I'll assume 5.x) I have do some wild guess here that you are missing the cascade_callbacks flag on your embedded relation.

class A
  include Mongoid::Document
  embeds_many :albums, cascade_callbacks: true
end
like image 54
Thomas R. Koll Avatar answered Dec 30 '22 02:12

Thomas R. Koll