I'm working on populating a jsonB field whenever a Candidate record is entered/moved to a new entrypoint.
I'm currently using the ff. approach:
(They can be called multiple times and will just append the new entry point to the jsonB field [entry])
// Method in Candidate model
def log_entry(value)
self.entry = Array(self.entry) << { val: value, timestamp: Time.now.to_i }
end
def log_entry!(value)
self.log_entry(value)
self.save
end
Called on every controller entrypoint
ca = Candidate.new(name: 'John Doe')
ca.log_entry('Point 1')
ca.save
Could also be called like this when moved to a new entry point
ca = Candidate.find id
ca.log_entry!('Point 2')
The record will look like this after calling log_entry twice:
Candidate Model
Name: 'John Doe'
Entry: [{ val: 'Point 1', timestamp: '<time>' }, { val: 'Point 2', timestamp: '<time>' }]
So far, the above approach is achieving the desired result. But I have an impression that the code is too cluttered and that there's a better approach for this.
Any ideas will be appreciated.