Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding custom attributes before save #241

Open
amoghh opened this issue Apr 16, 2020 · 1 comment
Open

Adding custom attributes before save #241

amoghh opened this issue Apr 16, 2020 · 1 comment
Labels

Comments

@amoghh
Copy link

amoghh commented Apr 16, 2020

I have a unique requirement. There is not many places i can ask this question because there is hardly any material on stackoverflow for this gem.
I have added history tracking to multiple models. What i want to do is to add my custom attributes to the history record before it is created. Is there any callback method which i can use to add attributes to the @history_tracker_attributes instance variable before it gets saved.
I do not want to add callbacks on the tracker class since i want to add different attributes based on the models for which history is being created.

@dblock
Copy link
Collaborator

dblock commented Apr 16, 2020

Does a custom changes method solve your issue?

(from the README)

class Foo
  include Mongoid::Document
  include Mongoid::History::Trackable

  attr_accessor :ip

  track_history on: [:ip], changes_method: :my_changes

  def my_changes
    unless ip.nil?
      changes.merge(ip: [nil, ip])
    else
      changes
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants