From 63ccf9ff07c7bae08dfd860e28728a5a4da5a3f3 Mon Sep 17 00:00:00 2001 From: Christian Gregg Date: Thu, 16 Aug 2018 14:28:17 +0100 Subject: [PATCH] Don't return association if table is nil --- CHANGELOG.md | 5 +++++ lib/ransack/adapters/active_record/context.rb | 2 +- spec/ransack/search_spec.rb | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41f1833..c0b4c93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## Unreleased +* Don't return association if table is nil + PR [952](https://github.com/activerecord-hackery/ransack/pull/952) + + *Christian Gregg* + ## Version 2.0.0 - 2018-08-09 * Add support for Active Record 5.2.1 diff --git a/lib/ransack/adapters/active_record/context.rb b/lib/ransack/adapters/active_record/context.rb index 037a517..9629bf5 100644 --- a/lib/ransack/adapters/active_record/context.rb +++ b/lib/ransack/adapters/active_record/context.rb @@ -268,7 +268,7 @@ module Ransack def find_association(name, parent = @base, klass = nil) @join_dependency.instance_variable_get(:@join_root).children.detect do |assoc| - assoc.reflection.name == name && + assoc.reflection.name == name && assoc.table && (@associations_pot.empty? || @associations_pot[assoc] == parent || !@associations_pot.key?(assoc)) && (!klass || assoc.reflection.klass == klass) end diff --git a/spec/ransack/search_spec.rb b/spec/ransack/search_spec.rb index a7e517e..afadcda 100644 --- a/spec/ransack/search_spec.rb +++ b/spec/ransack/search_spec.rb @@ -316,6 +316,11 @@ module Ransack .to eq s.result(distinct: true).send(all_or_load) end + it 'evaluates joins with belongs_to join' do + s = Person.joins(:parent).ransack(parent_name_eq: 'Ernie').result(distinct: true) + expect(s).to be_an ActiveRecord::Relation + end + private def remove_quotes_and_backticks(str)