Module: ActiveRecordAliasedJoins::JoinWithAlias

Defined in:
lib/active_record_aliased_joins.rb

Overview

Module to add joins_with_alias support to ActiveRecord::Base

Instance Method Summary collapse

Instance Method Details

#joins_with_alias(association_name, alias_name, join_klass = Arel::Nodes::InnerJoin) ⇒ Object

Raises:

  • (ActiveRecord::AssociationNotFoundError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/active_record_aliased_joins.rb', line 23

def joins_with_alias(association_name, alias_name, join_klass = Arel::Nodes::InnerJoin)
  reflection = _reflect_on_association(association_name.to_s)
  raise ActiveRecord::AssociationNotFoundError unless reflection

  source_joins =
    case reflection
    when ActiveRecord::Reflection::HasManyReflection, ActiveRecord::Reflection::HasOneReflection
      join_with_alias_has_many_has_one(reflection, association_name, alias_name, join_klass)
    when ActiveRecord::Reflection::BelongsToReflection
      join_with_alias_belongs_to(reflection, association_name, alias_name, join_klass)
    when ActiveRecord::Reflection::ThroughReflection, ActiveRecord::Reflection::HasAndBelongsToManyReflection
      join_with_alias_through(reflection, association_name, alias_name, join_klass)
    else
      raise UnsupportedReflectionError, reflection
    end

  joins(source_joins.join_sources)
end

#left_joins_with_alias(association_name, alias_name) ⇒ Object



42
43
44
# File 'lib/active_record_aliased_joins.rb', line 42

def left_joins_with_alias(association_name, alias_name)
  joins_with_alias(association_name, alias_name, Arel::Nodes::OuterJoin)
end