My app has the following models
class Metric < ApplicationRecord
belongs_to :post
def analyse!
client = ThirdPartyService.new
res = client.get_metrics post.body
# how to update itself here with the result of res?
end
end
class Post < ApplicationRecord
has_many :metrics
after_save :start_analysis
private
def start_analysis
metric = create_metric
metric.analysis!
end
end
The Metric table has a bunch of metrics, like:
adverbs_percentage
sentiment
and the returning result of calling:
client = ThirdPartyService.new
res = client.get_metrics post.body
is a hash containing those exactly props:
{
:adverbs_percentage => 10
:sentiment => 'Positive'
}
I would like to know how can I update the Metric from the analyse!
method, something like:
update_myself(res)
Thank you.