开发者

Eager loading a polymorphic association with Kaminara pagination

I'm trying to eager load a polymorphic association while also paginating using the Kaminari gem:

@news_items = NewsItem.includes(:news_source).not_outdated
.where(:group_id => group_ids).order("created_at DESC").page(params[:page]).per(10)

I'm getting the error message:

ActiveRecord::EagerLoadPolymorphicError in Pages#dashboard

Showing 'BLAH BLAH'/dashboard.html.erb where line #49 raised: Can not eagerly load the polymorphic association :news_source

When I remove the Kaminari scope ( .page[:page]).per(10) ), then the error disappears.

Anyone have any ideas? This article suggests that eager loading with polymorphic associations is supported, but only if the conditions/order that might be applied to the Relation as a scope don't reference any other tables (if they do, t开发者_运维百科hen Rails uses the LEFT OUTER JOIN method for the eager loading which can't work on polymorphic associations). So: does Kaminari reference another table?

Would appreciate any advice!

Cheers.


You should always use preload for polymorphic associations.

This error can happen because includes decides to call eager_load if it needs to query conditions on the association, and polymorphic associations are only supported by preload. It's in the documentation

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜