json_translate
json_translate copied to clipboard
Include the table name in the with_x_translation
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.
I like it!
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?