BUG 觸發條件
第一個觸發該 BUG 的情境是,載入過 association 後又改變其 foreign_key。
| post = Post.take | 
第二個觸發情境是,查詢過某個 association 是否載入過,卻沒有真的載入。
| post = Post.take | 
BUG 狀態
Rails 4.2.x 都有問題
Rails 5 後 belongs_to 已修復: #23498 - Don’t unnecessarily load a belongs_to when saving.。但 has_one 還未修復。
BUG 原因
在呼叫 association 時,會先從 association_cache 內找,假如找不到的話會 create 新的 association 物件,並寫入 association_cache。此時只會有 association 物件,model 資料還沒有載入,association.loaded? 為 false。ref
| def association(name) #:nodoc: | 
在物件儲存時,rails 會檢查哪些 association 需要 autosave。會去 association_cache 取出 association 物件,然後呼叫 load_target 取出 model 內容。但如果物件資料還沒有載入時,load_target 會下 query 去載入 model 資料。這其實是不需要的,因為假如子物件沒有載入過,代表子物件不可能有變動,不需要 autosave。ref
| 
 | 
Workarounds
可以在 initializers 內加上以下程式碼
| module ActiveRecord | 
用這個函式取代原本的寫法,以避開 BUG
| # before | 

