diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2015-10-30 09:42:02 -0600 |
---|---|---|
committer | Sean Griffin <sean@seantheprogrammer.com> | 2015-10-30 09:42:02 -0600 |
commit | e038975c29aa883cb9ac5472cdb0ea9319158121 (patch) | |
tree | a07d201488caaa064aa290b3d91d66fcfd703aa3 /activerecord/lib/active_record/relation/query_methods.rb | |
parent | 67417f18215349cad990b1230ecf591b5509d557 (diff) | |
parent | 3f46ef1ddab87482b730a3f53987e04308783d8b (diff) | |
download | rails-e038975c29aa883cb9ac5472cdb0ea9319158121.tar.gz rails-e038975c29aa883cb9ac5472cdb0ea9319158121.tar.bz2 rails-e038975c29aa883cb9ac5472cdb0ea9319158121.zip |
Merge pull request #12071 from Crunch09/outer_joins
added ActiveRecord::Relation#outer_joins
Diffstat (limited to 'activerecord/lib/active_record/relation/query_methods.rb')
-rw-r--r-- | activerecord/lib/active_record/relation/query_methods.rb | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index ad6c7fa2e5..27e7948b10 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -428,6 +428,25 @@ module ActiveRecord self end + # Performs a left outer joins on +args+: + # + # User.left_outer_joins(:posts) + # => SELECT "users".* FROM "users" LEFT OUTER JOIN "posts" ON "posts"."user_id" = "users"."id" + # + def left_outer_joins(*args) + check_if_method_has_arguments!(:left_outer_joins, args) + + args.compact! + args.flatten! + + spawn.left_outer_joins!(*args) + end + + def left_outer_joins!(*args) # :nodoc: + self.left_outer_joins_values += args + self + end + # Returns a new relation, which is the result of filtering the current relation # according to the conditions in the arguments. # @@ -878,6 +897,7 @@ module ActiveRecord arel = Arel::SelectManager.new(table) build_joins(arel, joins_values.flatten) unless joins_values.empty? + build_left_outer_joins(arel, left_outer_joins_values.flatten) unless left_outer_joins_values.empty? arel.where(where_clause.ast) unless where_clause.empty? arel.having(having_clause.ast) unless having_clause.empty? @@ -937,6 +957,19 @@ module ActiveRecord end end + def build_left_outer_joins(manager, outer_joins) + buckets = outer_joins.group_by do |join| + case join + when Hash, Symbol, Array + :association_join + else + raise ArgumentError, 'only Hash, Symbol and Array are allowed' + end + end + + build_join_query(manager, buckets, Arel::Nodes::OuterJoin) + end + def build_joins(manager, joins) buckets = joins.group_by do |join| case join @@ -952,6 +985,11 @@ module ActiveRecord raise 'unknown class: %s' % join.class.name end end + + build_join_query(manager, buckets, Arel::Nodes::InnerJoin) + end + + def build_join_query(manager, buckets, join_type) buckets.default = [] association_joins = buckets[:association_join] @@ -967,7 +1005,7 @@ module ActiveRecord join_list ) - join_infos = join_dependency.join_constraints stashed_association_joins + join_infos = join_dependency.join_constraints stashed_association_joins, join_type join_infos.each do |info| info.joins.each { |join| manager.from(join) } |