Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Globalize.with_locale in after_commit回调不会更改任何内容_Ruby On Rails_Callback_Globalize3_Rails I18n - Fatal编程技术网

Ruby on rails Globalize.with_locale in after_commit回调不会更改任何内容

Ruby on rails Globalize.with_locale in after_commit回调不会更改任何内容,ruby-on-rails,callback,globalize3,rails-i18n,Ruby On Rails,Callback,Globalize3,Rails I18n,在after_commit回调中更新缓存属性时,Globalize3 gem遇到问题 #In My Model after_commit :write_localized_caches private def write_localized_caches I18n.available_locales.each do |locale| Globalize.with_locale(locale) do self.write_attribute(:name, 'some loc

在after_commit回调中更新缓存属性时,Globalize3 gem遇到问题

#In My Model
after_commit :write_localized_caches
private
def write_localized_caches
  I18n.available_locales.each do |locale|
    Globalize.with_locale(locale) do
      self.write_attribute(:name, 'some localized string here')
    end
  end
end
它在_commit callbach之后启动,属性的值很好。但毕竟我的模特的名字还是空的

也许我对语言环境误用了
,或者有人遇到过同样的问题吗

更新1。 我肯定想使用after_commit回调对保存的对象执行复杂的查询。 在回调中打印出self.name将返回我想要的结果:“correct_string”。但id不会进入数据库。 最后写了一篇新的翻译作品。似乎Globalize在其底层使用回调:

def write_localized_caches
I18n.available_locales.each do |locale|
  Globalize.with_locale(locale) do
    self.translations.create!(name: 'some localized string here', locale: locale)
  end
end
end

这是可行的,但我还是觉得不对

在数据库完成记录保存后调用After commit

如果插入打印语句,如

def write_localized_caches
  puts self.name # the name that you're seeing in the database

  # Globalize Block

  puts self.name # The name that was set above most likely
end
还要记住,从回调返回false或nil将中止回调链并反转数据库事务。不过,after_commit是在事务完成后调用的,因此它在这里并不重要

在保存之前,您可能需要。

谢谢您的链接。由于复杂的查询,我绝对希望它发生在after_commit回调中。请参阅上面的更新。