Skip to content

Instantly share code, notes, and snippets.

View mbobin's full-sized avatar
🔧
side effects

Marius Bobin mbobin

🔧
side effects
View GitHub Profile
@mbobin
mbobin / active_record_extension.rb
Last active September 12, 2016 12:50 — forked from ozydingo/active_record_extension.rb
ActiveRecord left join
module ActiveRecordExtension
extend ActiveSupport::Concern
module ClassMethods
# Simple left join taking advantage of existing Rails & Arel code
def left_joins(*args)
inner_joins = self.unscoped.joins(*args).arel.join_sources
left_joins = inner_joins.map do |join|
Arel::Nodes::OuterJoin.new(join.left, join.right)
end