Ruby where with find_each
activerecord, ruby, ruby-on-rails
Solution
`Person.where("age > 21")` returns an ActiveRecord relation only. It doesn't return all the results.
`Person.where("age > 21").limit(10)` does NOT load all the models in memory, that would be awful and unusable. It just loads 10.
`find_each` doesn't really process 1000 records at a times. It loads 1000 records, and then process each one of them.
Problem
I am looking at the official Rails documentation which shows how to use the "find_each" method. Here is an example they gave ``` Person.where("age > 21").find_each do |person| person.party_all_night! end ``` This processes 1000 records at a time. However, I am still confused. How does this translate to SQL? What happens behind the scenes that allows Ruby to only process 1000 records at a time? The reason I am sort of confused is because it seems Person.where("age > 21") would execute first, which would return ALL results. For instance: ``` Person.where("age > 21").limit(10) ``` would return all persons in memory first, then give you the first 10, right?