From 0e585153eb19cfb777a42db46db7647ec85714f9 Mon Sep 17 00:00:00 2001 From: Jamie Davidson Date: Mon, 19 May 2014 22:48:37 -0400 Subject: [PATCH] Pretty poor attempt at a spec to showcase the 4.1 branch's issue with multiple joins on the same table --- spec/blueprints/recommendations.rb | 5 +++++ spec/ransack/search_spec.rb | 6 ++++++ spec/support/schema.rb | 12 ++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 spec/blueprints/recommendations.rb diff --git a/spec/blueprints/recommendations.rb b/spec/blueprints/recommendations.rb new file mode 100644 index 000000000..d416110e3 --- /dev/null +++ b/spec/blueprints/recommendations.rb @@ -0,0 +1,5 @@ +Recommendation.blueprint do + article + person + target_person { person } +end \ No newline at end of file diff --git a/spec/ransack/search_spec.rb b/spec/ransack/search_spec.rb index 247e220e2..3144ebf3e 100644 --- a/spec/ransack/search_spec.rb +++ b/spec/ransack/search_spec.rb @@ -183,6 +183,12 @@ module Ransack expect(where.to_sql).to match /#{children_people_name_field} = 'Ernie'/ end + it 'evaluates conditions for multiple belongs_to associations to the same table contextually' do + search = Search.new(Recommendation, person_name_eq: 'Ernie', target_person_parent_name_eq: 'Test') + search.result.should be_an ActiveRecord::Relation + search.result.to_sql.should == "SELECT \"recommendations\".* FROM \"recommendations\" LEFT OUTER JOIN \"people\" ON \"people\".\"id\" = \"recommendations\".\"person_id\" LEFT OUTER JOIN \"people\" \"target_people_recommendations\" ON \"target_people_recommendations\".\"id\" = \"recommendations\".\"target_person_id\" LEFT OUTER JOIN \"people\" \"parents_people\" ON \"parents_people\".\"id\" = \"target_people_recommendations\".\"parent_id\" WHERE ((\"people\".\"name\" = 'Ernie' AND \"parents_people\".\"name\" = 'Test'))" + end + it 'evaluates compound conditions contextually' do search = Search.new(Person, :children_name_or_name_eq => 'Ernie') expect(search.result).to be_an ActiveRecord::Relation diff --git a/spec/support/schema.rb b/spec/support/schema.rb index df9f5fb9d..8fa29c1d0 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -86,6 +86,12 @@ class Article < ActiveRecord::Base has_many :notes, :as => :notable end +class Recommendation < ActiveRecord::Base + belongs_to :person + belongs_to :target_person, class_name: 'Person' + belongs_to :article +end + module Namespace class Article < ::Article @@ -155,6 +161,12 @@ def self.create t.string :note end + create_table :recommendations, :force => true do |t| + t.integer :person_id + t.integer :target_person_id + t.integer :article_id + end + end 10.times do