In Rails, both find_each and where are used for retrieving data from Database supported by ActiveRecord.
You can pass your query condition to where
, like:
c = Category.where(:name => 'Ruby', :position => 1)
And you can pass batch size to find_each
, like:
Hedgehog.find_each(batch_size: 50).map{ |p| p.to_json }
But what's the difference between the following 2 code?
# code 1
Person.where("age > 21").find_each(batch_size: 50) do |person|
# processing
end
# code 2
Person.where("age > 21").each do |person|
# processing
end
Does code 1 batch retrieve 50 tuples each time, and code 2 retrieve all tuples in one time? More details explaination is welcomed.
My opinion is:
- both
where
andfind_each
can be used for batch retrieving, but user can define batch size when usingfind_each
. find_each
does not support passing query condition.
Please correct me if my understanding is wrong.
Aucun commentaire:
Enregistrer un commentaire