Skip to content

Commit

Permalink
Merge pull request globalizejs#489 from krisdigital/master
Browse files Browse the repository at this point in the history
Added uniq to with_translations to prevent duplicates when fallbacks are defined
  • Loading branch information
parndt committed Mar 1, 2016
2 parents 98ab1aa + c0147df commit 4f52fd2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/globalize/active_record/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def with_where(opts = {})

def with_translations(*locales)
locales = translated_locales if locales.empty?
preload(:translations).joins(:translations).readonly(false).with_locales(locales)
preload(:translations).joins(:translations).readonly(false).with_locales(locales).uniq
end

def with_required_attributes
Expand Down
21 changes: 21 additions & 0 deletions test/globalize/fallbacks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,27 @@ class FallbacksTest < MiniTest::Spec
assert_equal translations, user.name_translations
end
end

describe 'query with fallbacks' do
it 'does not result in duplicated records' do
I18n.fallbacks.clear
I18n.fallbacks.map :en => [ :de, :fr ]
I18n.locale = :en

product = Product.create(:name => 'foooooooo')
with_locale(:de) { product.name = 'bar' }
product.save!

assert_equal 1, Product.with_translations.where(id: product.id).length
assert_equal 'foooooooo', Product.find(product.id).name

I18n.locale = :de
assert_equal 'bar', Product.find(product.id).name

I18n.locale = :fr
assert_equal 'foooooooo', Product.find(product.id).name
end
end
end
# TODO should validate_presence_of take fallbacks into account? maybe we need
# an extra validation call, or more options for validate_presence_of.

0 comments on commit 4f52fd2

Please sign in to comment.