ruby-on-railsglobalize3

Rails, Globalize3 check if translated attributes changed?


In Rails we can check if a model attribute is changed, using obj.field_changed?.

Say I have a model that uses Globalize3.

It has translates :name

How can I check if the name attribute got changed?

Something like obj.name_changed?


Solution

  • I got the solution. I am posting it as an answer considering someone might get benefited.

    To check if the translated attributes got changed, just need to open Translation class provided by Globalize3 inside the corresponding model class.

    class Post < ActiveRecord::Base
      translates :name
      .......
    # Open the Translation class
      class Translation
        after_save do #you may define a private method as callback method
          if name_changed?
            #accomplish your tasks...
          end
        end
      end
    end