Knowledge for the World

webdev

All things web development.


How BEM works and why it's awesome

  • 2 favorites 2

Naming things is hard. "There are only two hard problems in Computer Science: cache invalidation and naming things." — Phil Karlton Truer words were never spoken.

How to identify and fix N+1 queries in your Rails app using Scout APM

  • 0 favorites 0

The N+1 problem occurs when at least two objects are stored in a database with a one-to-many relationship and queries are performed against the "one" table, then separately n number of times against the related object, n being the number of related objects.To illustrate this, I'll use the analogy of a meal website and a page where you wish to display the meals. In the controller:@meals = Meal.where(status=1) # approximate query: SELECT * FROM meals WHERE status = 1;In the view:<% @meal.each do |meals| %> <div class="meal-row"> <h4><%= meal.name %><br /> <%= link_to meal.user.username, user_path(meal.user.username) %> <%# approximate query: SELECT * FROM users WHERE user.id = {{ meal_id }}; %> </h4> <p><%= meal.description %></p> <%= link_to 'Cook This Now', meal %> </div> <% end %>Notice how we're creating a link to meal.user.username.To approximate the queries used in this page, we have the following:SELECT * FROM meals WHERE status = 1; # 1 time, yielding n rows SELECT * FROM users WHERE user.id = {{ meal_id }}; # n timesThe ORM in Rails should do a lot of work to minimize the net slowness of this problem such as database indexing and caching template fragments, but nothing will change the fact that you are querying the database in a loop.Now let's take a look at how to find these in your code and how to get rid of them.

Convert XML to JSON in PHP

  • 0 favorites 0

Converting XML files to JSON is a good practice for providing easy manipulation and preparing service responses. You can accomplish this efficiently with just three lines of code.

How to Install Vim Plugins Without a Plugin Manager

  • 0 favorites 0

Vundle, Pathogen, Vim-plug, VAM, and the others can be great time savers but sometimes you just have to do something yourself so you can understand what exactly the time saving software is doing for you in the background. For myself, I got sick of not understanding what these were doing to my system. I realized I did not know the fundamentals of vim plugins so I set out to learn.This is a step by step guide to teach you how to install a basic vim plugin without the help of any fancy manager.

Install bourbon in your rails project

  • 0 favorites 0

Bourbon is a library of Sass mixins. It provides a lot of functionality while keeping your css slim. Assuming you're using a standard rails installation, Sass will already be installed and working.

Using Python's simple HTTP server

  • 0 favorites 0

I started programming in PHP, so by default I would use apache even when I just wanted to test basic HTML files. As I was learning Python, I discovered a great HTTP request handler.

Add a text shadow using CSS3

  • 0 favorites 0

Text shadows are a beautiful and subtle effect for your web site. CSS3 text shadows are currently support on all versions of Chrome, Safari, Firefox 3.5+, and IE 10+.

How public key encryption works

  • 0 favorites 0

Public key encryption is also known as asymmetric because it requires two different keys - a public key and a private key. Encryption requires the public key, and decryption requires the private key.

Basic Git commands

  • 0 favorites 0

There are some fundamental git commands that you will use frequently that are hard to work into a "how-to" guide. Here are some basic git commands and how to use them. These are in no specific order.

How to commit and push in Git

  • 0 favorites 0

This guide will show you how to properly commit and push your work in Git. It is assumed that you have Git installed and that you're currently in a clean master branch.

How to build a free website

  • 0 favorites 0

There are a lot of free website builders, many of which are terribly difficult to use. I'm going to use WebStarts for this tutorial because I think it's the easiest to use.