1

I'm trying to run a query on table Testimonials which has a Translation Table TestimonialTranslations.

The Following query works like a charm:

Testimonial.with_translations(I18n.locale).where(:id => params[:id]).first

When I change the query to:

Testimonial.with_translations(I18n.locale).where(:alias => "test").first

It doesn't return any values?

A record exists where the where class is true:

=> [#<Testimonial id: 1, title: "Test", person: "", image_uid: nil, content: "<p>zfzefzfLorem ipsum dolor sit amet, consectetur a...", interest_group: "", created_at: "2015-01-15 11:48:11", updated_at: "2015-01-15 11:48:11", job: "", overview: true, content_short: "<p>Lorem ipsum dolor sit amet, consectetur adipisci...", hidden: false, hide_image: false, alias: "test">]

I know 100% sure that the language is "nl" and that it returns a query when I run:

Testimonial.with_translations(I18n.locale)

These are my specs:

  • ruby 1.9.3p550 (2014-10-27 revision 48165) [x86_64-darwin13.4.0]
  • Rails 3.2.19

EDIT 1:

I'm going to leave this open for a while but as far as I can see it is not possible to add a where to the with_translations query that will go and look in the translation table.

With this knowledge I will need to do 2 querys.

FastSolutions
  • 1,809
  • 1
  • 25
  • 49

2 Answers2

2

you could try to add to_sql in order to check what sql query is generating

Testimonial.with_translations(I18n.locale).where(:alias => "test").to_sql
alvaritono
  • 121
  • 3
  • "SELECT \"testimonials\".* FROM \"testimonials\" WHERE \"testimonial_translations\".\"locale\" = 'nl' AND \"testimonials\".\"alias\" = 'test'" It seems to be doing the where on the testimonials instead of on the testimonials_translations – FastSolutions Jan 15 '15 at 12:24
  • Are you using globalize gem? Checking the code a little bit, I think you could use the method `with_translated_attribute`. – alvaritono Jan 15 '15 at 12:44
  • Here is the line: https://github.com/globalize/globalize/blob/eccb924ac9641b52399f22525b0e3ec004739f4c/lib/globalize/active_record/class_methods.rb#L22 – alvaritono Jan 15 '15 at 12:45
0

Did you try with with_translated_attribute ?

alvaritono
  • 121
  • 3
  • That worked t.b.h. @testimonial = Testimonial.with_translated_attribute(:alias, params[:id].downcase, I18n.locale).first – FastSolutions Jan 15 '15 at 13:32