diff --git a/lib/json_translate/translates.rb b/lib/json_translate/translates.rb index 7f154bf..8fdc241 100644 --- a/lib/json_translate/translates.rb +++ b/lib/json_translate/translates.rb @@ -39,7 +39,7 @@ def translates(*attrs, allow_blank: false) end define_singleton_method "with_#{attr_name}_translation" do |value, locale = I18n.locale| - quoted_translation_store = connection.quote_column_name("#{attr_name}#{SUFFIX}") + quoted_translation_store = connection.quote_table_name("#{self.table_name}.#{attr_name}#{SUFFIX}") translation_hash = { "#{locale}" => value } if MYSQL_ADAPTERS.include?(connection.adapter_name) diff --git a/test/test_helper.rb b/test/test_helper.rb index 5305b8e..1531ccf 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -7,7 +7,15 @@ I18n.available_locales = [:en, :fr] class Post < ActiveRecord::Base + has_many :tags translates :title, :body_1 + + scope :tagged, -> (tag_title) { joins(:tags).merge(Tag.with_title_translation(tag_title)) } +end + +class Tag < ActiveRecord::Base + belongs_to :post + translates :title end class PostDetailed < Post @@ -53,6 +61,10 @@ def create_table t.column :body_1_translations, column_type t.column :comment_translations, column_type end + connection.create_table(:tags, :force => true) do |t| + t.column :title_translations, column_type + t.column :post_id, :integer + end end end diff --git a/test/translates_test.rb b/test/translates_test.rb index 8e4672e..dd20855 100644 --- a/test/translates_test.rb +++ b/test/translates_test.rb @@ -238,6 +238,14 @@ def test_with_translation_relation end end + def test_with_translation_when_ambiguous_column + p = Post.create!(:title_translations => { "en" => "Alice in Wonderland", "fr" => "Alice au pays des merveilles" }) + Tag.create!(:title_translations => { "en" => "A Tag", "fr" => "Un tag" }, post: p) + I18n.with_locale(:en) do + assert_equal p.title_en, Post.tagged("A Tag").first.try(:title) + end + end + def test_with_interpolation_arguments p = Post.create!(:title_translations => { "en" => "Alice in %{where}" }) I18n.with_locale(:en) do