当我试图通过指定的艺术家获取所有音频歌曲时,我收到错误:
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')
我使用以下AR语句:
Audio.with_translations(I18n.locale).find(:all, :conditions => ["artist = ?", 'Andy'])
没有with_translations
方法:
>> 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">]
答案 0 :(得分:3)
尝试改变
Audio.with_translations(I18n.locale).find(:all, :conditions =>
["artist = ?", 'Andy'])
到
Audio.with_translations(I18n.locale).find(:all, :conditions =>
["audios.artist = ? OR audio_translations.artist ?", 'Andy', 'Andy'])
此查询执行两个具有相同列的表的SQL连接
答案 1 :(得分:3)
ActiveRecord StatementInvalid是不明确的是AR的说法: Mister,你的SQL语句没有意义,因为你正在检查的字段在几个表上找到,我不知道哪个使用。
尝试在任何地方指定表名称,例如audios.artist
和audio_translations.artist
。