spree-contrib / spree_i18n

I18n translation files for Spree Commerce.
http://guides.spreecommerce.org
BSD 3-Clause "New" or "Revised" License
348 stars 762 forks source link

ActiveRecord's pluck & Globalize issue ? #656

Open dunric opened 8 years ago

dunric commented 8 years ago

Can anybody explain why equivalent requests produce different results ?

1) fetch value of Spree::ProductProperty

irb> Spree::ProductProperty.where(id: 55).first.value
  Spree::ProductProperty Load (1.1ms)  SELECT  "spree_product_properties".* FROM "spree_product_properties" WHERE "spree_product_properties"."id" = $1  ORDER BY spree_product_properties.position LIMIT 1  [["id", 55]]
  Spree::ProductProperty::Translation Load (0.5ms)  SELECT "spree_product_property_translations".* FROM "spree_product_property_translations" WHERE "spree_product_property_translations"."spree_product_property_id" = $1  [["spree_product_property_id", 55]]
=> "Pepsi-Cola"

2) fetch the same value, limit query with pluck

> Spree::ProductProperty.where(id: 55).pluck(:value)
   (0.9ms)  SELECT "spree_product_properties"."value" FROM "spree_product_properties" WHERE "spree_product_properties"."id" = $1  ORDER BY spree_product_properties.position  [["id", 55]]
=> [nil]

See, related translation table is not joined automatically so we get untranslated (empty) result.

3) fetch the same value with pluck but now with translation inlcuded

> Spree::ProductProperty.joins(:translations).where(id: 55).pluck('spree_product_property_translations.value')
   (0.9ms)  SELECT spree_product_property_translations.value FROM "spree_product_properties" INNER JOIN "spree_product_property_translations" ON "spree_product_property_translations"."spree_product_property_id" = "spree_product_properties"."id" WHERE "spree_product_properties"."id" = $1  ORDER BY spree_product_properties.position  [["id", 55]]
=> ["--- Pepsi-Cola\n...\n"]

What ?! Why the translation string is "--- Pepsi-Cola\n...\n" and not just "Pepsi-Cola" like in the 1st paragraph ?

woto commented 8 years ago

Hi, just sorted out with translation problems (distantly related with you problem) and decided to check you bug report. Here is result:

irb(main):065:0> Spree::ProductProperty.where(id: 8).first.value
  Spree::ProductProperty Load (0.5ms)  SELECT  "spree_product_properties".* FROM "spree_product_properties" WHERE "spree_product_properties"."id" = $1  ORDER BY spree_product_properties.position LIMIT 1  [["id", 8]]
  Spree::ProductProperty::Translation Load (8.2ms)  SELECT "spree_product_property_translations".* FROM "spree_product_property_translations" WHERE "spree_product_property_translations"."spree_product_property_id" = $1  [["spree_product_property_id", 8]]
=> "Blue"
irb(main):066:0> Spree::ProductProperty.where(id: 8).pluck(:value)
   (7.9ms)  SELECT "spree_product_properties"."value" FROM "spree_product_properties" WHERE "spree_product_properties"."id" = $1  ORDER BY spree_product_properties.position  [["id", 8]]
=> [nil]
irb(main):067:0> Spree::ProductProperty.joins(:translations).where(id: 8).pluck('spree_product_property_translations.value')
   (23.8ms)  SELECT spree_product_property_translations.value FROM "spree_product_properties" INNER JOIN "spree_product_property_translations" ON "spree_product_property_translations"."spree_product_property_id" = "spree_product_properties"."id" WHERE "spree_product_properties"."id" = $1  ORDER BY spree_product_properties.position  [["id", 8]]
=> ["Синий", "Blue"]
GIT
  remote: git://github.com/spree/spree.git
  revision: ac4d1d019026a63852cbebed140ba52945f1b8d8
  branch: 3-0-stable
  specs:
    spree (3.0.8)
      spree_api (= 3.0.8)
      spree_backend (= 3.0.8)
      spree_cmd (= 3.0.8)
      spree_core (= 3.0.8)
      spree_frontend (= 3.0.8)
      spree_sample (= 3.0.8)
    spree_api (3.0.8)
      rabl (~> 0.11.6)
      spree_core (= 3.0.8)
      versioncake (~> 2.3.1)
    spree_backend (3.0.8)
      bootstrap-sass (~> 3.3)
      jquery-rails (~> 4.1)
      jquery-ui-rails (~> 5.0)
      select2-rails (= 3.5.9.1)
      spree_api (= 3.0.8)
      spree_core (= 3.0.8)
    spree_cmd (3.0.8)
      thor (~> 0.14)
    spree_core (3.0.8)
      activemerchant (~> 1.47.0)
      acts_as_list (= 0.7.2)
      awesome_nested_set (~> 3.0.1)
      cancancan (~> 1.10.1)
      carmen (~> 1.0.0)
      deface (~> 1.0.0)
      ffaker (~> 1.16)
      font-awesome-rails (~> 4.0)
      friendly_id (~> 5.1.0)
      highline (~> 1.6.18)
      json (~> 1.7)
      kaminari (~> 0.15, >= 0.15.1)
      monetize (~> 1.1)
      paperclip (~> 4.3.0)
      paranoia (~> 2.1.0)
      premailer-rails
      rails (~> 4.2.2)
      ransack (~> 1.4.1)
      responders
      sprockets-rails
      state_machines-activerecord (~> 0.2)
      stringex
      truncate_html (= 0.9.2)
      twitter_cldr (~> 3.0)
    spree_frontend (3.0.8)
      bootstrap-sass (>= 3.3.5.1, < 3.4)
      canonical-rails (~> 0.0.4)
      jquery-rails (~> 4.1)
      spree_api (= 3.0.8)
      spree_core (= 3.0.8)
    spree_sample (3.0.8)
      spree_core (= 3.0.8)