簡體   English   中英

如何在STI子類上急於加載has_one關聯

[英]How to eager-load has_one associations on STI subclass

我有一個這樣的模型層次結構:

class Activity
end

class HelloActivity < Activity
  has_one :hello_activity_details
end

class WorldActivity < Activity
  has_one :world_activity_details
end

我想查詢所有活動並渴望加載詳細信息以避免N + 1問題。 但是,請執行以下操作:

Activity.all.include([:hello_activity_details, :world_activity_details])

產量

Association named 'hello_activity_details' not found on WorldActivity; perhaps you misspelled it?

如何加載詳細信息?

我知道我可以手動調用預加載器,但是還有一種慣用的方式。

謝謝

錯誤狀態看起來像是典型的錯字錯誤。

has_one的類關聯名稱必須為singular

嘗試:

class HelloActivity < Activity
  has_one :hello_activity_detail
end

class WorldActivity < Activity
  has_one :world_activity_detail
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM