Notes posted by flah00
RSS feed
0 thanks
When dealing with has_many through
The non-repeating primary key id must be used with find_in_batches.
-
User has many things
-
User has many socks through things
-
Sock has many things
-
Sock has many users through things
For the sake of argument, assume the first user has two socks and all other users have one sock. There are 1000 users in total and 1001 socks in total.
User.joins(:socks).count => 1001 agg = [] # Incorrect User.joins(:socks).find_in_batches{|g| agg += g} agg.count => 1000 Sock.joins(:users).count => 1001 agg = [] # Correct Sock.joins(:users).find_in_batches{|g| agg += g} agg.count => 1001