Data runs most technology businesses these days.
Fortunately, rails and Active Record makes it pretty easy to organize our tables and postgres helps us make sure the tables stay consistent.
Recently I was looking for a more efficient way to query data, and I love how easy it is to
pluck data when i need it.
Specifically loved that I could scope and group and pluck in a single query.
So we’re looking to group by month, and scope this to an account pretty straight forward…
But then I needed to represent more data on the page.
All good and fine, as you can see pretty reasonable requests… “Show as much data as helpful”…
Could It Be More Efficient?
Plucking multiple columns seemed like it should have a native API.
But the current API feels awkward:
Not Bad, but whats that first thing? and setting multiple variables in a single line is kinda odd depending on your project.
How would I want it to look like?
This also seemed pretty reasonable, so I created pluck_multi.rb and started writing some code:
And it worked like a charm!
This little Gem ended up being super simple, and a super cool way for me to be like “wow, i actually improved my own life a tiny little bit“…
I put this file inside /lib/extensions/pluck_multi.rb
Then created an initializer which required all files in the extension folder:
After including these files, all my Models got slightly more helpful for me, and thats how my love story with rails goes…