Ruby error - extconf.rb failed while installing sqlite3 gem

I ran into some issues while running 'bundle install' on a production server. This occured when bundler hit the 'sqlite3' gem.

At first glance, I found the line

  checking for sqlite3.h... no

So naturally I did the firs thing I could think of. Oh I need to install sqlite3 on this server.

  sudo apt-get install sqlite3

Unfortunately this did not work. I now had sqlite3 on the server (which undoubtidly would have casued me issues later), but the issue was still laughing at me.

After a bit of Googling, I was able to find a Stack Overflow question that helped me out.

So in the end it turns out that I just needed to install the libsqlite3-dev package. I should have know this as I had ran into this issue with MySql in the past. O'well.

  sudo apt-get install libsqlite3-dev

Cache Ruby Variables

Get user from the database only if @current_user does not already exist.

class ApplicationController
    @current_user ||= User.find(session[:user_id])
end

Performance improvement ensues.

Polymorphic Associations

Polymorphic Associations are a method for dealing with database models that have some sort of logical relationship. One model can belong to many other models on a single association. What this means is that a model can have many one-to-one relationships with other models. Lets see if an example explains this a bit better.

I am going to try and explain this in a way that is framework agnostic. The concept will hopefully be clear without being laden with details for a specific implementation.

Polymorphic Orders

Lets say you have an ordering system where users can come into a store, buy online, or pick out products from a mail order catalog. You handle this service as a merchant for many different sites, stores, and catalogs. You need to track all orders along with the source.

To do this polymorphically you will need four tables:

orders:
    - id
    - price
    - tax
    - orderable_id
    - orderable_type

websites:
    - id
    - name

stores:
    - id
    - name

catalogs:
    - id
    - name

Knowing that polymorphic associations allow you to tie models together on one association made you decide this was the perfect model, because you can only have a purchase at one store with one method at a time. Brilliant!

If you paid attention you might have noticed that orders has some awkwardly named columns that are not really words, but they make me sound smart. These columns are where the magic happens.

Column: orderable_id

The orderable_id column is what links the polymorphic association to the correct record id of whatever model it happens to be used with. This in essence functions like a foreign key.

Column: orderable_type

The orderable_type column will be a string name representing the class name of the model it is linking to.

Order Shipped:

The frameworks that use these types of associations all provide a different API for utilizing them. I am not sure if this is based on a pattern or if they are simply mirroring functionality because it is done elsewhere and expected. If anyone knows I would really like to hear about that.

These associations allow you to work both ways, if you have a record you can get to the parent type or the linking type.

order = Order.find(10)

# get the polymorphic parent
order.orderable

website = websites.find(100)

# get all of the orders
website.orders

Package Delivered:

Hopefully I did a good enough job explaining this in an abstract enough way that you should at least have an understanding of what Polymorphic Associations are. Note there are also many-to-many representations of these associations which I will cover in another post.

Deleting branches in Git

Deleting branches in Git is something that we all have to do from time to time. It really is an easy task to do but I end up having to search for the syntax to do it every time.

Deleting a local branch
git branch -D [branch_name]

The command really is not that much different than it's counterpart for creating a new local branch.

Creating a local branch
git checkout -b [branch_name]

The problem I always have is with deleting remote branches. Sometimes remote branches live very short lives. We push code to have others look at, then we merge with master. This leaves us with a remote branch to dispose of as it is no longer needed.

Deleting a remote branch
git push origin :[branch_name]

There is a new way to delete branches which will hopefully stick, although I can assure I will still have to look this up as well. The concept of pushing to nothing to a remote branch to delete it makes sense but I also think the git api could be a bit more clear on it's intentions.

Deleting a remote branch
git push origin --delete [branchName]

There you have it. Two ways to delete a remote branch as well as a way to delete a local branch. And lucky you I even threw in how to create a branch with the checkout command.



Related Topics

  • Git

Installing Laravel 4 on Ubuntu

I figured I would take the time to write this up as it took me a little bit of fudging to get through it. I had never installed the stack to run Laravel on a production instance and it showed. Up until this point I have done my development on the Laravel Homestead Vagrant box.

Server

This will work on any virtual server but I chose to got with a Digital Ocean Droplet. I chose to go with them for a mix of price, comfort, and performance. I have had several Droplets in the past and they all seemed to just work. I have never had an issue with them and the price really can't be beat for what you get.

Software

Below you fill find the steps that I went through to create my instance. I will give the little hints for the sections that I encountered some issues in the hopes that you don't. Also, I have condensed the steps into as few as possible to make it easier to follow along without the out of order steps that I took. The stack you will be installing will contain:

  • PHP 5
  • MySQL
  • Apache2
  • Git

Steps

The first thing you will want to do after you have a droplet created it sot make sure your OS has a reference to the latest packages from its repositories.

Updage Packages Lists
sudo apt-get upate

Now you are ready to install all of the packages needed.

Install Packages
sudo apt-get install git git-core php5 php5-mysql libapache2-mod-php5 mysql-server libmysqlclient-dev curl php5-mcrypt

Your software stack is now installed. You will still need to install composer but for the most part you are done. A little issue I had was that they module for mcrypt was not installed by default. I had to figure out how to get that installed and configured. Luckily it was not that hard, I included it in the install step earlier; but you still need to enable it.

Enable PHP-5 Mcrypt
sudo php5enmod mcrypt
Installing Composer
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

You are now setup and ready to create your new Laravel 4 project. Enjoy.



Related Topics

  • Digital Ocean
  • Laravel
  • Sudo
  • apt-get
  • Ubuntu
  • Apache
  • PHP
  • MySQL
  • Git
  • Composer