Type a keyword and hit enter to start searching. Press Esc to cancel.

Welcome to My Blog, Hope You Find Something Helpful

The perfect database schema is one where you don’t need to concede form to any obscure requirements. In that perfect scenario all associated models could be individually identified with a single foreign key.   According to the Rails guides, you can only define an association’s foreign key with a single column. We live in an imperfect world with many obscure business requirements, so sometimes we need to use two keys.  One common scenario is when you need to centralize and synchronize data from multiple outside API’s.  Luckily, there is a solution. This example will be based on an Entity Attribute […]

ActiveRecord is awesome! However, it’s not without a few downsides and common pitfalls. The simplicity sometimes comes at a cost of efficiency. A great example is the n+1 problem when scoping an ActiveRecord relation by a belongs_to association. For example, take the following associated Models class Employee < ActiveRecord::Base belongs_to :company end class Company < ActiveRecord::Base has_many :employees has_many :offices end If you were using an Employee instance method to find company offices for the Employee, you may do something like this: Office.where(company: self.company) Or you could say: Office.where(company_id: self.company_id) Both examples will give you the following query: SELECT * FROM offices […]

Referential integrity, it gives me that warm and fuzzy feeling inside.  That’s why I was so excited when Rails added support for managing foreign keys with ActiveRecord.  However, I got a little confused when implementing a foreign key on a Model that had an internal tree structure with self-referential parent/child associations.  Here’s the final implementation. Take the following Model with a self-referential parent / child association: class Category < ActiveRecord::BaseActiveRecord::Base belongs_to :parent, class: ‘Category’ has_many :children, class: ‘Category’, foreign_key: ‘parent_id’ end To add a foreign key for that association we would put the following in our migration: add_reference :categories, :parent, […]

Rails uniqueness validations are not thread safe!  This is a problem when even single threaded Rails servers are susceptible to race conditions.  Luckily, this is an easy problem to avoid. If you have a multi-threaded Rails server like Puma or Passenger Enterprise, use asynchronous background processing with tools like Sidekiq, or have a load balanced environment with multiple web servers then the race condition is real!  Just defining a uniqueness validation will not keep your data unique and can cause a lot of headaches. First, let’s look at why they are not thread safe with a simple example.  Take the […]

Creating and managing your own private dedicated ARK: Survival Evolved server is a great way to enjoy the game with friends.  Server rentals can be very expensive too.  Luckily, you can rent your own virtual private server (VPS) from Digital Ocean where you can install and manage your own ARK server for about $10/month.  This guide will walk you through the process and it assumes you don’t have much, if any at all, experience in managing remote servers. If you haven’t done so already, you will need to signup for an account with Digital Ocean, https://www.digitalocean.com/?refcode=77d53f9e1f35.  Once you created your account […]

Using an AngularJS based frontend, paired with a Rails backend, is a great stack.  AngularJS provides $resource, which is a mostly complete module to interface with the Rails backend and provide CRUD actions.  However, if you are using strong parameters with a require statement in Rails, then $resource can be a bit of a pain. When generating forms with ActionView, you use form_for @model, this will generate input names with a name like name=”model[attribute]”.  Using this same naming scheme in AngularJS does not work, because the input names are not correctly interpreted as a JSON object. So, instead of using $resource, […]

ActiveRecord is great.  It helps hide away a lot of trivial SQL when creating applications with Rails.  It’s fairly good at what it does to and it’s constantly growing and improving.  However, it falls short when it comes to creating a query that combines multiple calculations.  As an example, let’s get an array of the total sales for every month for the last 12 months of Orders. totals = [] 12.times do |i| month = i.months.ago totals << Order.where(‘created_at >= ? AND created_at <= ?’, month.beginning_of_month, month.end_of_month).sum(:total) end This is going to run 12 queries in order to get the […]

Shower is a gem for Ruby on Rails 4 that simplifies message streaming, the ability to push data to the front end in real time.  ActionController::Live is a new feature in Rails 4 that allows for message streaming.  Essentially, a user opens a persistent connection to a Live controller and messages are pushed through the connection.  While instant messaging seems to be the standard ‘Hello World’ introduction to streaming, there are many more uses.  For example, inventory notifications for an online store, stock prices, breaking news, maintenance alerts, etc.  As message streaming finds more uses, more and more clients will expect to […]