I have an UserProfile model:
class UserProfile < ApplicationRecord
after_update :check_changes
def check_changes
AuditRecord.create(account_id: self.id, fields: self.saved_changes , account_type: 'UserProfile', admin_id: 3) if self.saved_changes?
end
| id | user_id | name | last_name |
|---|---|---|---|
| 1 | 1 | john | doe |
| 2 | 2 | foo | bar |
and AuditRecord model:
| id | account_type | account_id | field | admin_id |
|---|---|---|---|---|
| 1 | UserProfile | 1 | {} | 3 |
| 2 | UserProfile | 2 | {} | 3 |
This AuditRecord saves all of the updates of the profiles, but could be updated by different admins.
How can I send to check_changes function the admin_id? Because right now always is going to be 3.
Admin, which is related toAuditRecordin some way.Adminmodel, set up the appropriate active record association and references. Rails docs for thisadmin.auditrecordsdepending upon how you set up the associations.