westonganger / active_snapshot

Simplified snapshots and restoration for ActiveRecord models and associations with a transparent white-box implementation
MIT License
103 stars 16 forks source link

How to track nested changes? #3

Closed dhruv-shipmnts closed 2 years ago

dhruv-shipmnts commented 3 years ago

Let's say Model A -> has many B -> has many C How to generate a snapshot having details of A, B, and C all 3?

westonganger commented 3 years ago

What you desire can be accomplished using the following code example:

class B < ApplicationRecord
  has_many :c_items
end

class A < ApplicationRecord
  has_many :b_items

  has_snapshot_children do
    instance = self.class.includes(:comments, :ip_address).find(id)

    {
      b_items: b_items,
      c_items: b_items.flat_map{|x| x.c_items },
    }
  end

end