mardi 28 février 2017

Rails - run method only after a specific attribute's JSON field is updated

I'm trying to run a method in Rails only after the JSON field search_terms of the settings attribute is updated.

The model's name is Campaign and this is an example of an instance's attributes:

Campaign.first.attributes

=> {
               "id" => 1,
             "name" => "example.com",
           "status" => "on",
        "languages" => [],
          "country" => nil,
         "settings" => {
                   "search_terms" => {
            "happy" => true,
            "sad" => true,
              "excited" => true
        },
        "keyword_refresh_intervals" => {
            "en-us" => 60,
            "ru-ru" => "0",
            "en-gb" => "0"
        }
    },
    "advertiser_id" => 1,
       "created_at" => Sun, 25 Dec 2016 10:55:47 UTC +00:00,
       "updated_at" => Tue, 28 Feb 2017 08:07:05 UTC +00:00
}

In the Campaign model itself I'm executing the required Job after a commit is made:

class Campaign < ApplicationRecord
  after_commit -> { PlacementsGeneratorJob.perform_later }

But I want it to be executed only after the search_terms value is changed in the settings attribute and not when the whole settings attribute is changed.

Is that even possible?

Aucun commentaire:

Enregistrer un commentaire