ruby-on-rails
  • postgresql
  • activerecord
  • globalize3
  • 2011-07-12 88 views 5 likes 
    5

    當我試圖通過指定藝術家讓所有的音頻歌曲中獲取數據的時候,我得到錯誤:ActiveRecord的StatementInvalid與globalize3

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

    回答

    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連接具有相同的列兩個表的

    3

    ActiveRecord的StatementInvalid是模糊的是說的敷設方式:先生,你的SQL語句不是有道理的,因爲你正在檢查的領域是在幾張桌子上找到的,我不知道用哪一種

    嘗試在任何地方指定表名,例如audios.artistaudio_translations.artist

    相關問題