Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord StatementInvalid when getting data with globalize3

When I'm trying to get all Audio songs by specified artist, I get error:

ActiveRecord::StatementInvalid: PGError: ERROR:  column reference "artist" is ambiguous
LINE 1: ... AND (audio_translations.artist IS NOT NULL) AND (artist = '...
                                                             ^
: SELECT "audios"."id" AS t0_r0, "audios"."audio_genre_id" AS t0_r1, "audios"."song" AS t0_r2, "audios"."artist" AS t0_r3, "audios"."file_id" AS t0_r4, "audios"."photo_id" AS t0_r5, "audios"."description" AS t0_r6, "audios"."position" AS t0_r7, "audios"."created_at" AS t0_r8, "audios"."updated_at" AS t0_r9, "audios"."date" AS t0_r10, "audio_translations"."id" AS t1_r0, "audio_translations"."audio_id" AS t1_r1, "audio_translations"."locale" AS t1_r2, "audio_translations"."artist" AS t1_r3, "audio_translations"."song" AS t1_r4, "audio_translations"."description" AS t1_r5, "audio_translations"."created_at" AS t1_r6, "audio_translations"."updated_at" AS t1_r7 FROM "audios" LEFT OUTER JOIN "audio_translations" ON "audio_translations"."audio_id" = "audios"."id" WHERE "audio_translations"."locale" = 'en' AND (audio_translations.artist IS NOT NULL) AND (artist = 'Andy')

I use the following AR statement:

Audio.with_translations(I18n.locale).find(:all, :conditions => ["artist = ?", 'Andy'])

It works without with_translations method:

 >> Audio.find(:all, :conditions => ["artist = ?", 'Andy'])
 => [#<Audio id: 10, audio_genre_id: 1, song: "My heart, my life", artist: "Andy", file_id: 1, photo_id: nil, description: "...", position: 2, created_at: "2011-07-12 07:24:43", updated_at: "2011-07-12 08:31:21", date: "2011-07-12 07:24:00">] 
like image 319
NARKOZ Avatar asked Jul 12 '11 09:07

NARKOZ


2 Answers

try to change

Audio.with_translations(I18n.locale).find(:all, :conditions => 
  ["artist = ?", 'Andy'])

to

Audio.with_translations(I18n.locale).find(:all, :conditions => 
  ["audios.artist = ? OR audio_translations.artist ?", 'Andy', 'Andy'])

this query does sql join of two tables that have same columns

like image 60
Bohdan Avatar answered Sep 25 '22 00:09

Bohdan


ActiveRecord StatementInvalid is ambiguous is ARs way of saying: Mister, your SQL statement does not make sense because a field you are examining is found on several tables and I don't know which to use.

Try specifying the table name everywhere, e.g. audios.artist and audio_translations.artist.

like image 45
thomax Avatar answered Sep 24 '22 00:09

thomax