Module: LeftJoin::Adapters::ActiveRecordAdapter

Defined in:
lib/left_join/adapters/active_record_adapter.rb

Instance Method Summary collapse

Instance Method Details

#left_join(*columns) ⇒ Object Also known as: left_joins

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)


14
15
16
17
18
19
20
21
22
23
# File 'lib/left_join/adapters/active_record_adapter.rb', line 14

def left_join(*columns)
  join_dependency = ActiveRecord::Associations::JoinDependency.new(self, columns, [])
  if RAILS4_1_PLUS
    joins(join_dependency)
  else
    join_dependency.join_associations.inject(self) do |result, association|
      association.join_relation(result)
    end
  end
end