ActiveRecord may be both the best and worst part of Rails. Currently the largest scaling problem that I'm facing is with all the before_* and after_* callbacks which run per model record rather than a batch of changed records. This is an N+1 query landmine field for which ActiveRecord offers no solutions.
I agree that ActiveRecord isn't particularly opinionated about how to deal with updates to batches of records, but there are multiple ways of approaching this and AR won't get in your way.
upsert_all[1] is available to update a batch of records in a single write that does not invoke model callbacks.
activerecord-import[2] is also very nice gem that provides a great api for working with batches of records.
It can be as simple as extracting your callback logic and a method (def self.batch_update) and running your callback logic after the upsert.
By upsert_all not invoking model callbacks, it's admitting that the ActiveRecord approach doesn't scale.
"It can be as simple as extracting your callback..." Isn't this the kind of repetitive thing a framework should be doing on your behalf?
To be fair, ActiveRecord isn't a fault Rails invented. Apparently it's from one of Martin Fowler's many writings where each model instance manages its own storage. Even Fowler seems to say that the DataMapper approach is better to separate concerns in complex scenarios.
TBF no framework will do everything perfectly, and having clean escape hatches is pretty good in itself.
Even outside of batch processing, there will usually be a few queries that absolutely benefit from being rewritten in a lower layer of the ORM or even plain SQL. It's just a fact of life.
This is not an art exhibition. Conventions have a purpose. Capitalization is not a random spelling quirk, there’s a reason why we have it. The upvote balance on my comment confirms that it’s something to consider. The “fuck you” was in reference to the author’s comment, not just to his blog post.
This is a post in a personal blog that they wrote because they wanted to write it. Someone else came across it and pointed people outside of it's desired audience that it existed.
If I make a peanut butter and jelly sandwich and offer it to my friend next to me, and someone across the street yells "How rude of you! I'm allergic to peanuts", I will just close the window and continue sharing sandwiches with my friend, not spend time arguing with the yelling guy across the street.
reply