Skip to content

Instantly share code, notes, and snippets.

@diego-aslz
Last active May 10, 2016 15:59
Show Gist options
  • Save diego-aslz/b867cd87d580e97549f2 to your computer and use it in GitHub Desktop.
Save diego-aslz/b867cd87d580e97549f2 to your computer and use it in GitHub Desktop.
Method to do a LEFT OUTER JOIN on an ActiveRecord model class.
class ActiveRecord::Base
# Does a left join through an association. Usage:
#
# Book.left_join(:category)
# # SELECT "books".* FROM "books"
# # LEFT OUTER JOIN "categories"
# # ON "books"."category_id" = "categories"."id"
#
# It also works through association's associations, like `joins` does:
#
# Book.left_join(category: :master_category)
def self.left_join(*columns)
_do_left_join columns.compact.flatten
end
private
def self._do_left_join(column, this = self) # :nodoc:
collection = self
if column.is_a? Array
column.each do |col|
collection = collection._do_left_join(col, this)
end
elsif column.is_a? Hash
column.each do |key, value|
assoc = this.reflect_on_association(key)
raise "#{this} has no association: #{key}." unless assoc
collection = collection._left_join(assoc)
collection = collection._do_left_join value, assoc.klass
end
else
assoc = this.reflect_on_association(column)
raise "#{this} has no association: #{column}." unless assoc
collection = collection._left_join(assoc)
end
collection
end
def self._left_join(assoc) # :nodoc:
source = assoc.active_record.arel_table
pk = assoc.association_primary_key.to_sym
joins source.join(assoc.klass.arel_table,
Arel::Nodes::OuterJoin).on(source[assoc.foreign_key].eq(
assoc.klass.arel_table[pk])).join_sources
end
end
@johncalvinyoung
Copy link

Would be nice to know what license you intend for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment