diff --git a/Gemfile b/Gemfile index 5977ce0..c786238 100644 --- a/Gemfile +++ b/Gemfile @@ -17,7 +17,7 @@ rails_version = case rails gem 'faker', '~> 0.9.5' gem 'sqlite3', ::Gem::Version.new(rails_version) >= ::Gem::Version.new('6-0-stable') ? '~> 1.4.1' : '~> 1.3.3' gem 'pg', '~> 1.0' -gem 'pry', '0.10' +gem 'pry', '~> 0.12.2' gem 'byebug' case rails diff --git a/spec/ransack/adapters/active_record/context_spec.rb b/spec/ransack/adapters/active_record/context_spec.rb index 282e381..dd7f37f 100644 --- a/spec/ransack/adapters/active_record/context_spec.rb +++ b/spec/ransack/adapters/active_record/context_spec.rb @@ -79,6 +79,25 @@ module Ransack expect(constraint.right.relation.name).to eql 'people' expect(constraint.right.name).to eql 'id' end + + it 'build correlated subquery for multiple conditions (default scope)' do + search = Search.new(Person, { comments_body_not_eq: 'some_title'}) + + # Was + # SELECT "people".* FROM "people" WHERE "people"."id" NOT IN ( + # SELECT "comments"."disabled" FROM "comments" + # WHERE "comments"."disabled" = "people"."id" + # AND NOT ("comments"."body" != 'some_title') + # ) ORDER BY "people"."id" DESC + # Should Be + # SELECT "people".* FROM "people" WHERE "people"."id" NOT IN ( + # SELECT "comments"."person_id" FROM "comments" + # WHERE "comments"."person_id" = "people"."id" + # AND NOT ("comments"."body" != 'some_title') + # ) ORDER BY "people"."id" DESC + + expect(search.result.to_sql).to include '"comments"."person_id" = "people"."id"' + end end describe 'sharing context across searches' do diff --git a/spec/support/schema.rb b/spec/support/schema.rb index ca0277c..70df22b 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -164,6 +164,8 @@ end class Comment < ActiveRecord::Base belongs_to :article belongs_to :person + + default_scope { where(disabled: false) } end class Tag < ActiveRecord::Base @@ -209,6 +211,7 @@ module Schema t.integer :article_id t.integer :person_id t.text :body + t.boolean :disabled, default: false end create_table :tags, force: true do |t|