ruby-on-railsrubyactiverecordglobalize

Raw SQL queries when using the Globalize gem with Ruby on Rails


I am using the ruby gem Globalize to for translation purposes in a Ruby on Rails app. I have a model, Region, which I am translating the name column on. When using ActiveRecord to query the regions table, such as:

Region.where(name: 'translated')

The region record with the translated name translated is correctly returned. However, when I query with:

Region.where('name like ?', 'translated')

It returns an empty ActiveRecord_Relation. It seems that querying with SQL strings accesses database records directly without utilizing the Globalize gem.

How can I perform like queries that will retrieve translated records?

(I am working with Globalize 5.0.0 and Rails 4.2.8)


Solution

  • The solution I got to work for me was:

    Region.with_translations(I18n.locale).where('region_translations.name like ?', "translated")

    This joins the region_translations table on region_translations.region_id = regions.id, where I18n.locale = region_translations.locale