我有兩個模型,Post
和Tag
,使用has_and_belongs_to_many
關聯。我有一個動作來加載特定標籤的帖子,我想急切地加載每個帖子的其他標籤。Rails急切加載has_and_belongs_to_many
我試圖在Tag
模型使用
has_and_belongs_to_many :posts, -> { includes :tags }
,並在控制器動作用
@posts = @tag.posts.where(approved: true).includes(:tags)
試過,但在這兩種情況下,渲染時N + 1次的查詢被運行
post.tags.pluck(:name).join(', ')
在視圖中。
有什麼我做錯了,或者我不能使用急於加載引用到同一個表時?
它不應該是.includes(:標籤),具有複數包括? – Danny
我的不好,實際上是在我的代碼中。我現在編輯帖子 –