0

I have a form where I have 3 edits (one for each language). When I check afterwards, the text doesn't get inserted in the DB ok. (I get 3 times the same text).

This is in the controller of ingredient_category, here it calls the update_other_locals_for after each insert or update, this gets called alright.

after_filter lambda { |controller| controller.update_other_locals_for(@ingredient_category) }, :only => [:create, :update]

This is the code in the ApplicationController that gets called

  available_locals.each do |available_locale|
    I18n.locale = available_locale
    params_object = "#{available_locale}_" + item.class.to_s.underscore.downcase
    if params[params_object.to_sym].present?
      item.update_attributes(params[params_object.to_sym])
    end
  end

The update line is getting hit and the correct text is getting to that point but when I'm debugging I see this in the query:

Started PUT "/ingredient_categories/4" for 127.0.0.1 at 2013-06-20 21:58:34 +0200
Processing by IngredientCategoriesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"aZFuv8v19oZcBkDmUzRzNyMUhBXnL5X0WvSyxNZhEuQ=", "ingredient_category"=>{"name"=>"Dutch"}, "en_ingredient_category"=>{"name"=>"English"}, "fr_ingredient_category"=>{"name"=>"French"}, "id"=>"4"}
  Shop Load (0.8ms)  SELECT "shops".* FROM "shops" WHERE "shops"."subdomain" = '' LIMIT 1
  User Load (1.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 27 LIMIT 1
  IngredientCategory Load (0.5ms)  SELECT "ingredient_categories".* FROM "ingredient_categories" WHERE "ingredient_categories"."id" = ? LIMIT 1  [["id", "4"]]
  CACHE (0.1ms)  SELECT "ingredient_categories".* FROM "ingredient_categories" WHERE "ingredient_categories"."id" = ? LIMIT 1  [["id", "4"]]
   (0.3ms)  begin transaction
  IngredientCategory::Translation Load (0.6ms)  SELECT "ingredient_category_translations".* FROM "ingredient_category_translations" WHERE "ingredient_category_translations"."ingredient_category_id" = 4
   (0.5ms)  UPDATE "ingredient_categories" SET "updated_at" = '2013-06-20 19:58:34.880306' WHERE "ingredient_categories"."id" = 4
  SQL (3.2ms)  INSERT INTO "ingredient_category_translations" ("created_at", "ingredient_category_id", "locale", "name", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", Thu, 20 Jun 2013 21:58:34 CEST +02:00], ["ingredient_category_id", 4], ["locale", "en"], ["name", nil], ["updated_at", Thu, 20 Jun 2013 21:58:34 CEST +02:00]]
  IngredientCategory::Translation Load (0.5ms)  SELECT "ingredient_category_translations".* FROM "ingredient_category_translations" WHERE "ingredient_category_translations"."ingredient_category_id" = 4 AND "ingredient_category_translations"."locale" = 'en' LIMIT 1
   (0.7ms)  UPDATE "ingredient_category_translations" SET "name" = 'Dutch', "updated_at" = '2013-06-20 19:58:34.918413' WHERE "ingredient_category_translations"."id" = 31
  IngredientCategory::Translation Load (0.4ms)  SELECT "ingredient_category_translations".* FROM "ingredient_category_translations" WHERE "ingredient_category_translations"."id" = ? LIMIT 1  [["id", 31]]
   (2.2ms)  commit transaction
Redirected to http://127.0.0.1:3000/ingredient_categories
   (0.3ms)  begin transaction
   (0.6ms)  UPDATE "ingredient_categories" SET "updated_at" = '2013-06-20 20:00:23.273350' WHERE "ingredient_categories"."id" = 4
  IngredientCategory::Translation Load (0.4ms)  SELECT "ingredient_category_translations".* FROM "ingredient_category_translations" WHERE "ingredient_category_translations"."ingredient_category_id" = 4 AND "ingredient_category_translations"."locale" = 'en' LIMIT 1
   (0.4ms)  UPDATE "ingredient_category_translations" SET "name" = 'English', "updated_at" = '2013-06-20 20:00:23.294591' WHERE "ingredient_category_translations"."id" = 31
  IngredientCategory::Translation Load (0.4ms)  SELECT "ingredient_category_translations".* FROM "ingredient_category_translations" WHERE "ingredient_category_translations"."id" = ? LIMIT 1  [["id", 31]]
   (3.2ms)  commit transaction
   (0.3ms)  begin transaction
   (0.5ms)  UPDATE "ingredient_categories" SET "updated_at" = '2013-06-20 20:01:19.871801' WHERE "ingredient_categories"."id" = 4
  IngredientCategory::Translation Load (0.6ms)  SELECT "ingredient_category_translations".* FROM "ingredient_category_translations" WHERE "ingredient_category_translations"."ingredient_category_id" = 4 AND "ingredient_category_translations"."locale" = 'en' LIMIT 1
   (0.4ms)  UPDATE "ingredient_category_translations" SET "name" = 'French', "updated_at" = '2013-06-20 20:01:19.892798' WHERE "ingredient_category_translations"."id" = 31
  IngredientCategory::Translation Load (0.4ms)  SELECT "ingredient_category_translations".* FROM "ingredient_category_translations" WHERE "ingredient_category_translations"."id" = ? LIMIT 1  [["id", 31]]
   (1.1ms)  commit transaction
Completed 302 Found in 187809ms (ActiveRecord: 19.2ms)

I noticed this:

AND "ingredient_category_translations"."locale" = 'en'

on all the updates. Why isn't it setting the corresponding languages?

rept
  • 2,086
  • 1
  • 26
  • 44

1 Answers1

0

It seems to be a bad idea to do the updates by changing the I18n.locale. If you do it with Globalize.with_locale(available_locale) it works like a charm!

  available_locals.each do |available_locale|
    Globalize.with_locale(available_locale) do
      params_object = "#{available_locale}_" + item.class.to_s.underscore.downcase
      if params[params_object.to_sym].present?
        item.update_attributes(params[params_object.to_sym])
      end
    end
  end
rept
  • 2,086
  • 1
  • 26
  • 44