3
所以..只是從3.0.6升級到3.0.14,我的一個測試開始失敗(太棒了!)。它指出(我認爲)我的where子句是錯誤地形成的,而我剛剛'盡力避開它'。只是想確認(如果有人能指出我可以在哪裏找到有關記錄此更改的文檔)。Rails - 3.0.6到3.0.14 activerecord,多個連接和where子句的怪異
一些代碼:
@fields = Field.joins(:region => :country).where(:regions => {:country_id => @country}).order(:name)
產生快樂的SQL
"SELECT `fields`.* FROM `fields` INNER JOIN `regions` ON `regions`.`id` = `fields`.`region_id` INNER JOIN `countries` ON `countries`.`id` = `regions`.`country_id` WHERE `regions`.`country_id` = 5 ORDER BY name"
但此查詢...
@pools = Pool.joins(:field => {:region => :country}).where(:fields => {:regions => {:country_id => @country }},:confirmed => true).order(:leaving_date)
這將產生好的SQL在3.0.6
"SELECT `pools`.* FROM `pools` INNER JOIN `fields` ON `fields`.`id` = `pools`.`field_id` INNER JOIN `regions` ON `regions`.`id` = `fields`.`region_id` INNER JOIN `countries` ON `countries`.`id` = `regions`.`country_id` WHERE `pools`.`confirmed` = 1 AND `regions`.`country_id` = 5 ORDER BY leaving_date"
但在3.0.14它先後爲國家ID的borked值(似乎是整個活動記錄?)
"SELECT `pools`.* FROM `pools` INNER JOIN `fields` ON `fields`.`id` = `pools`.`field_id` INNER JOIN `regions` ON `regions`.`id` = `fields`.`region_id` INNER JOIN `countries` ON `countries`.`id` = `regions`.`country_id` WHERE `pools`.`confirmed` = 1 AND `fields`.`regions` = '--- \\n:country_id: !ruby/ActiveRecord:Country \\n attributes: \\n name: Test Country 1\\n latitude: \\\"13.456\\\"\\n created_at: 2012-06-27 10:28:46 Z\\n updated_at: 2012-06-27 10:28:46 Z\\n id: 2\\n cached_slug: test-country-1\\n longitude: \\\"46.789\\\"\\n message: \\n zoom: 4.0\\n' ORDER BY leaving_date"
只是想知道如果任何人能解釋這是怎麼回事。我懷疑這可能應該從來沒有工作過..
非常好!是的,自從第一次寫這篇文章以來,我學到了更多的rails ......所以我(希望)我不會再犯這個錯誤了!感謝您的澄清。我想我感到驚訝的是,我認爲在一個小版本的變化中,api本身不應該改變。但是,正如你所說,它應該永遠不會起作用;) –