From 4e43a994c284608d7078b6430a4110c72b38a231 Mon Sep 17 00:00:00 2001 From: Anton Antonov Date: Wed, 15 Feb 2017 03:16:52 +0200 Subject: [PATCH] Fix left_outer_joins not being respected Given a relation that has `#left_outer_joins` before calling `#search`, ``` Person.left_outer_joins(:article).search(article_name_cont: 'foo') ``` Ransack will perform a `LEFT OUTER JOIN` twice resulting into an invalid query. Since Rails 5.x.x added a `left_outer_joins_values`, `Context#build` must take the existing joins from there too before automagically adding them. This mimics the behavior added at https://github.com/rails/rails/blob/ef7b9b867b3c113bbbc7639b5d760a8f962a683c/activerecord/lib/active_record/relation/query_methods.rb#L943 --- lib/ransack/adapters/active_record/context.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/ransack/adapters/active_record/context.rb b/lib/ransack/adapters/active_record/context.rb index 390664a4d..d3ba65e2d 100644 --- a/lib/ransack/adapters/active_record/context.rb +++ b/lib/ransack/adapters/active_record/context.rb @@ -250,7 +250,10 @@ def join_dependency(relation) # Checkout active_record/relation/query_methods.rb +build_joins+ for # reference. Lots of duplicated code maybe we can avoid it def build_joins(relation) - buckets = relation.joins_values.group_by do |join| + buckets = relation.joins_values + buckets += relation.left_outer_joins_values if ::ActiveRecord::VERSION::MAJOR >= 5 + + buckets = buckets.group_by do |join| case join when String :string_join