-
Use find_each vs. select or each when needing to process each record via Ruby
The problem: This scenario is a much more practical case of a similar concept with Rails / ActiveRecord count, size, and length. In this case, you’re needing to run ruby code off of either every value from a where method result or a subset that is determined by ruby code. For example, assume that a […]
-
Rails / ActiveRecord count, size, and length
When trying to be sensitive to n+1 queries and memory usage, knowing the differences between count, size, and length in ActiveRecord is important. It had been a while since I reviewed the usage, and I wanted to ensure that I hadn’t made some bad assumptions along the way that somehow stuck. The reality is that […]
-
Getting a count of the rows in all of your ActiveRecord models
Warning: Going through ActiveRecord is likely not something you want to do in production unless you have a replica database that your script or your model can point at. See the second part for MySQL and PostgreSQL examples in SQL. How do you enumerate your models? For apps created under Rails 5 app or later, […]