Follow Along as I Stumble on the Path to Learning RoR

Latest

Automatically Handle N+1 Queries

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.

Advertisements

Improving ActiveAdmin Performance

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

filter :title
filter :created_at

(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:

index do
column :title
column :description
column :created_at
column :updated_at
end

 

 

Dump from PostgreSQL Using PGAdmin

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.

PostgreSQL Backup Database - pgadmin backup

pgAdmin backup options

pgAdmin backup dump options 1

pgAdmin backup dump options 2

pgAdmin backup messages

 

CSS Only Image Overlay

While this isn’t a Rails solution, it was one that was so helpful to me that I wanted to post about it.  I needed a tinted/filtered image overlay with text.  After Binging the internet, I found the solution courtesy of Koren Leslie Cohen.

You can read her post here to see a demo, but you wrap your image, like this:

  <div id=“outer-box”>
    <img src=“cartagena.jpg”>
    <div id=“inner-box”>
      <p>This is the overlay!</p>
    </div>
  </div>
and then add this CSS,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#outer-box {
  width: 300px;
  height: 194px;
  display: block;
  position: relative;
}
#outer-box img {
  width: 300px;
  height: auto;
}
#outer-box #inner-box {
  background: #FD8698;
  height: 100%;
  width: 100%;
  opacity: 0;
  top: 0;
  left: 0;
  position: absolute;
  padding: 0;
  transition: opacity .5s;
}
#outer-box #inner-box p {
  color: #fff;
  lineheight: 150px;
  fontfamily: ‘arial’;
  textalign: center;
}
#outer-box:hover #inner-box {
  opacity: .9;
  transition: opacity .5s;
}

This will show the overlay when the image is hovered over.  For me, I wanted to show the overlay without the hover, so I changed line 33 to be:

#outer-box #inner-box {

Creating Sortable Bootstrap Tables

I found a great article on sorting bootstrap tables without any gems or additional libraries.  Check it out here.

Hiding Values from Appearing in Rails Logs

I had some fields that were sensitive, so I didn’t want the values appearing in the logs.  The solution was filtering them.  Doing that was easy.

In config/initializers/filter_parameter_logging.rb, and your fields, like so:

Rails.application.config.filter_parameters += [:password, :secret_field]

Now, when I look at the logs, I see this:

“user”=>{“password”=>”[FILTERED]”, “secret_field”=>”[FILTERED]”

Adding Heroku Postgresql to PGAdmin

To add your Heroku DB to PGAdmin, first get your credentials from Heroku.

You’ll need your:

  • host
  • DB name
  • port
  • user name
  • password

Add a new server using these credentials.  In addition, you’ll want to change the server settings, or you’ll see about a thousand DB names in your list.

  1. make sure you are disconnected from the server,
  2. select the Server, right click -> properties, Advanced tab,
  3. in ‘DB restriction’ type in the name of your database(s) enclosed in single quotes and separated by spaces (i.e. ‘db_name’).

Now you’re server should be able to connect and you should just see your DB in the “Databases” section under your server.