开发者

ActiveRecord StatementInvalid when getting data with globalize3

开发者 https://www.devze.com 2023-03-19 09:33 出处:网络
When I\'m trying to get all Audio songs by specified artist, I get error: ActiveRecord::StatementInvalid: PGError: ERROR:column reference \"artist\" is ambiguous

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">] 


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号