I needed to abbreviate numbers in Rails so that they were shorter and easier to read. Rails has a great built in method, number_to_human. However, I didn’t like how number_to_human displays without any customization.
My main issue was that instead of spelling out the entire number, I just wanted to put a letter abbreviation. In addition, I didn’t want a space between the number and its text description.
For example, “out of the box”, if you do:
You will get:
Instead, I wanted 1.23K.
Luckily, I found this StackOverflow answer by jdoe.
To format it like this, you just need to add these values to config/locales/en.yml (or whatever you’re using as your 18n locale):
(under "en:") number: human: decimal_units: format: "%n%u" units: unit: "" thousand: K million: M billion: B trillion: T quadrillion: Q
I found a gem that’s helping me a bunch, so I just wanted to give it a quick mention. After asking people for help on a N+1 query that I couldn’t figure out, one of the people recommended checking out the Goldiloader gem.
I was so impressed that the gem solved my N+1 query so easily, I decided to use it across my site.
It’s ridiculously simple to use once you have model associations defined.
All you need to do is to add the gem to your gemfile and it does the eager loading for you! If there are places you want to avoid eager loading, you can disable Goldiloader for a query.
ActiveAdmin is my favorite gem for creating admin features in Rails. It makes it so easy to create views to monitor and manage apps. Much of the heavy lifting is done for you by them gem.
I found that I need to do some tuning on my ActiveAdmin views to get the functionality I wanted without placing too heavy a burden on my server.
I mainly did this by doing two simple things:
(1) Specify Filters – if you don’t specify your filters, you may get far too many (and consuming too many resources when you don’t need to). Specifying filters are easy, just go into you admin files for the resource (i.e. app > admin > article.rb), then add a line for each filter you want
(2) Specify Index (and other) Columns – similar to above, if you don’t specify values, you may be receiving too much data. Be more efficient by only showing what you need to see in a view.
You can do this in your admin file for a resource, simply by adding this:
I was having trouble figuring out how to dump the data from PostgreSQL with PGAdmin until I have this on PostgreSQL Tutorial.
Following the directions, I was able to do a dump as .tar file. They also have instructions on how to restore it.
I found a great article on sorting bootstrap tables without any gems or additional libraries. Check it out here.