Ruby on rails Rails 5:当组合急切的\u加载、左\u连接和限制时,返回数据集的数量会出现问题

Ruby on rails Rails 5:当组合急切的\u加载、左\u连接和限制时,返回数据集的数量会出现问题,ruby-on-rails,pagination,left-join,limit,Ruby On Rails,Pagination,Left Join,Limit,我正在显示一个列表(带有分页和不同的搜索功能),其中包含3种对象: class Tree < ApplicationRecord has_many :samples end class Sample < ApplicationRecord belongs_to :tree has_many :projects_samples has_many :projects, through: :projects_samples end class Project <

我正在显示一个列表(带有分页和不同的搜索功能),其中包含3种对象:

class Tree < ApplicationRecord
  has_many :samples
end

class Sample < ApplicationRecord
  belongs_to :tree
  has_many :projects_samples
  has_many :projects, through: :projects_samples
end

class Project < ApplicationRecord
  has_many :projects_samples
  has_many :samples, through: :projects_samples
end
我得到的只有2个数据集。 当我从第一个样本中删除一个项目时,我得到了3个。再添加一个项目只返回一个结果

计算结果时也存在差异:

result.length #2
result.size #2
result.count #3
当我注意到我的paginator gem(将paginate 3.1.6)在第一页上只显示6个条目而不是10个条目时,我偶然发现了这种行为

Rails 5.1.5、Ruby 2.5.0


感谢您的帮助

观察由
示例生成的SQL查询。渴望加载(:树)。左连接(:项目)。限制(3)
@Pavel Mikhailyuk,我已经做了。查询正常,并在复制到postgres终端时返回正确的记录数(3)!记录不等于模型实例。仔细观察,3条记录中有多少不同的样本ID很好,你是对的,输出中只有2个不同的样本ID。。。无论如何,这还不能帮助我解决这个问题。我能做的就是使用includes而不是left_连接来获得正确数量的结果。但是,项目的搜索结果只包含匹配的项目。
result.length #2
result.size #2
result.count #3