0
包括在的has_many所有记录考虑下面的代码为什么find_in_batches失败通过关系
class Things < ActiveRecord::Base
belongs_to :user
belongs_to :sock
end
class User < ActiveRecord::Base
has_many :things
has_many :socks, through: :things
end
class Sock < ActiveRecord::Base
has_many :things
has_many :users, through: :things
end
假设第一个用户有两种袜子和所有其他用户有一个袜子。总共有1000个用户和1001个袜子。您会希望find_in_batches
返回与正常选择相同数量的记录。
User.joins(:socks).count
=> 1001
agg = []
User.joins(:socks).find_in_batches{|g| agg += g}
agg.count
=> 1000