rails: Eager loading associations that use limit, group, and offset produces incorrect results

Eager loading any of the following associations will products incorrect results:

class Blog < ActiveRecord::Base
  has_many :limited_posts, -> { order(:id).limit(2) }, class_name: 'Post'
  has_many :grouped_posts, -> { group(:blog_id) }, class_name: 'Post'
  has_many :offset_posts, -> { offset(2) }, class_name: 'Post'
end

Test case can be found in https://gist.github.com/jturkel/1e6355e8de2bf9953945.

Probably best to just disallow eager loading in these cases.

About this issue

  • Original URL
  • State: open
  • Created 10 years ago
  • Reactions: 4
  • Comments: 16 (13 by maintainers)

Commits related to this issue

Most upvoted comments

It looks like the group eager loading issue was fixed in Rails 5.0.7, 5.1.5 and 5.2.0 with https://github.com/rails/rails/pull/29413 🎉

I’ve added a more reliable test case here which shows eager loading associations with a limit or offset returning incorrect results.

Nobody is working on it but we know it is a bug. It would be awesome if you could try to fix it.

I’m still seeing this on Rails 5.0.0.beta3