Iterating through every record in a database - Ruby on Rails / ActiveRecord
Also a possible one-liner for same purpose:
User.all.map { |u| u.number = ""; puts u.number }
Here is the correct syntax to iterate over all User :
User.all.each do |user|
#the code here is called once for each user
# user is accessible by 'user' variable
# WARNING: User.all performs poorly with large datasets
end
To improve performance and decrease load, use User.find_each
(see doc) instead of User.all
. Note that using find_each
loses the ability to sort.