Skip to content

Changes to embedded documents not tracked in parent history #187

@mpetazzoni

Description

@mpetazzoni

I'm trying to track all changes to a model object and its embedded documents, as described in the "Include embedded objects attributes in parent audit" section of the README.

My models roughly looks like this:

class ParentModel
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::History::Trackable
  include Mongoid::Userstamp
  mongoid_userstamp user_model: 'User'

  field :foo
  embeds_one :child
  accepts_nested_attributes_for :child

  track_history on: [:fields, :updated_by_id, :embedded_relations],
                modifier_field: :modifier,
                modifier_field_inverse_of: :nil,
                version_field: :version,
                track_create: true,
                track_update: true,
                track_destroy: false
end

class ChildModel
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Userstamp
  mongoid_userstamp user_model: 'User'

  field :bar
  embedded_in :parent, inverse_of: :child
end

Changes to fields of the parent are correctly tracked in the parent's history, but not changes to fields of the child.

> p = Parent.all().first
> p.name = "test"
> p.save
> p.history_tracks.reverse.first
### shows the change to the name in the parent document

> p.child.bar = "test"
> p.save
> p.history_tracks.reverse.first
### no additional audit trail entry recorded, this shows the change to the name of the parent from above

Am I misunderstanding how the tracking of embedded relations is supposed to work? Does it only track a change to the reference itself, for example if I change p.child to a completely different child object?

Is there a way for me to accomplish what I'm looking for here, that is to have a single audit trail of parent document that shows all changes to all its fields, and all the fields of its embedded relations?

I was expecting #150 to provide this functionality.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions