Join

Joining a table to itself in Ruby on Rails 3 when you can’t use the Id

Mar 31, 2011

First of all let me explain that joining a table to itself normally – i.e. using primary and foreign keys, is pretty simple and can be done using the lovely ActiveRecord belongs_to and has_many association. That isn’t what I am talking about here, I am interested in joining to the same table using other fields and conditions. Why would you want to do this? Well in my case it is the first step in shortlisting customers in the database who have potential duplicates.