I have the following Rails Query:
Org.find(:all).each do |org|
Org.update_counters org.id, :users_count => org.users.length
end
For various reasons, like performance, I need to write this in SQL, so I can execute SQL, and not user Rails to make the update. Any ideas how combing the rails loger into sql is possible?
Thanks
This:
Basically does this:
Unrolling one step:
Now you’ve wrapped that in an
Org.find(:all).eachso we just have to push the iteration into the SQL and deal with#{org.id}:And if you really mean to set the
users_countvalues rather than increment them: