ruby on rails - Globalize.with_locale in after_commit callback does not change anything -
i'm having troubles globalize3 gem when updating cached attribute in after_commit callback.
#in model after_commit :write_localized_caches private def write_localized_caches i18n.available_locales.each |locale| globalize.with_locale(locale) self.write_attribute(:name, 'some localized string here') end end end
it launches after_commit callbach , value attribute fine. after model's name still empty!
maybe i'm misusing with_locale
or faced same problem?
update 1. want use after_commit callback perform complex queries on saved objects. printing out self.name inside callback returns want: 'correct_string'. id not hit database. ended writing new translation creation. seems globalize uses callback in basement:
def write_localized_caches i18n.available_locales.each |locale| globalize.with_locale(locale) self.translations.create!(name: 'some localized string here', locale: locale) end end end
this works, still not feel right me!
after commit called after database has completed saving record.
if insert print statements such as
def write_localized_caches puts self.name # name you're seeing in database # globalize block puts self.name # name set above end
also keep in mind returning false or nil callback abort callback chain , reverse database transaction. though, after_commit called after transaction complete, matters less here.
you want before_save. http://api.rubyonrails.org/classes/activerecord/callbacks.html
Comments
Post a Comment