json_translate icon indicating copy to clipboard operation
json_translate copied to clipboard

Include the table name in the with_x_translation

Open louim opened this issue 6 years ago • 2 comments

When generating with_#{attr_name}_translation methods, include the table name in the query. This is to prevent a possible ambiguous column error when the model is joined with another model having the same translated column name.

For example:

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

Calling Post.tagged("abc") would produce ActiveRecord::StatementInvalid: PG::AmbiguousColumn: ERROR: column reference "title_translations" is ambiguous because the table name was not included in the query. I added a test covering that case.

louim avatar Jul 31 '19 17:07 louim

I like it!

frantisekrokusek avatar Jan 05 '21 10:01 frantisekrokusek

Hey @frantisekrokusek, I rebased this PR against the main branch. It seems like travis is not setup to run the tests anymore. Are you planning on switching to another CI tool setup?

louim avatar Feb 23 '21 21:02 louim