reminds me of an anecdote from Le Ride (https://en.wikipedia.org/wiki/Le_Ride ) that talks about in one of the early Tours de France, a rider was disqualified because he broke his crank, hobbled to a nearby town, found the blacksmith, and the rider welded his own crank back together, but the blacksmith's son operated the bellows and this was seen as too much external assistance.
My guess is that they're referring to how Rails will instantiate full objects for ActiveRecord database requests and this is often not what you want, because we often just want a single or a few attributes from the model.
So if you had a model Activity, and you wanted to get the names of some of them, the slower/higher memory approach would be something like:
Activity.where("id < 100").map(&:name)
This pulls results from the database, instantiates each as Activity objects, then iterates through each to get its name. You can see in the logs that it grabs the entire object:
Activity Load (133.7ms) SELECT "activities".* FROM "activities" WHERE (id < 100)
.pluck instead grabs just the field you're looking for without instantiating the entire object. So the better way to grab the list of activity names would be like:
Activity.where("id < 100").pluck(:name)
And you can see from the logs that it makes a more narrow query:
(3.8ms) SELECT "activities"."name" FROM "activities" WHERE (id < 100)
Awesome thanks for the reply, am making an effort to learn more activerecord and didn’t know pluck would function like this. Was playing around with it last night on a ruby hash and missed the memory advantages
Not at all surprised that Laurenz Albe played a key role in figuring this out -- I've learned so much about postgres from his Stack Overflow answers over the years.
So this is kind of tangentially related here, but is there a "state-of-the-art" or best practices on digging through big JS apps / frameworks / objects? Ultimately this is all code running on my machine, so it feels like it should be accessible in the console somewhere.
I've had to do a fair amount of this with a Chrome extension I maintain that hooks in to routing (creating bike / running routes) sites, and fortunately these mostly use React and I'm decently familiar with how to hook in to that.
But is there a limit here? Is there a way to actually keep code from being inaccessible to the console (aside from obfuscation / compiling to wasm)?
Not in a way that differs from old-fashioned web scraping. If the website you're targeting is built using a framework and is server-side rendered then it'll most definitely also send all the data it needs for hydrating on the client as JSON, usually attached to the window object as "window.__INITIAL_STATE__" or similar. There are no limits :–)
I'm working in vue, and get lost at the transition between my code and the framework stuff, when an error "falls through the cracks", and the call stack comes from the framework. What do you do for that, even?
wow, thanks for the thorough reply. Atlanta's pawpaw population is taking off and they're fun to introduce to people, but I've been hesitant to do so more lately knowing that they may not be 100% good for you. seems like it's actually not so bad in moderation.
Yeah my personal rule of thumb is not to eat more than two per day and six per week, and to periodically check Google Scholar for new research. If you're going to cook with them, I'd recommend sticking to recipes that involve adding them to cooked food (e.g. as a layer on top of a cheesecake) rather than applying heat directly to them. And also to eat them only during the month or so per year that they're in season, rather than freezing them and eating them year round.
And if you haven't already seen it, here are Neal Peterson's recommendations for safe consumption:
His recommendations are notable mainly because he's the one who kickstarted the pawpaw renaissance and released many of the most popular (and best) cultivars on the market today.
Parents eat it at least once a week. They have a tree that bears so such it broke it's head off due to the load once when I was over there.The tree are hallow and not terribly strong.
I think you're safe.
Surprised the efficiency is so low. Those Wh/mi numbers are comparable to a Tesla Model 3 or Model Y, vehicles that presumably weigh substantially more.