Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polymorphic search doesn't take polymorphic type into the query #1120

Closed
Looooong opened this issue May 7, 2020 · 1 comment · Fixed by #1122
Closed

Polymorphic search doesn't take polymorphic type into the query #1120

Looooong opened this issue May 7, 2020 · 1 comment · Fixed by #1122

Comments

@Looooong
Copy link

Looooong commented May 7, 2020

Given the polymorphic example from the wiki:

Location.ransack(locatable_of_House_type_number_eq: 100).result

The final query doesn't compare locatable_type = 'House'. This would gives false result, as the House id can match IDs from other polymorphic types as well.

Here is the full specs:

# You can run this file directly using `ruby` command
# Given bundler and rspec 3 are installed globally
# It might be different if you are using rspec 2
require 'bundler/inline'
require 'rspec/autorun'
require 'rspec/core/formatters/progress_formatter'
require 'rspec/core/profiler'

# TODO: Define dependencies
gemfile true do
  source 'https://rubygems.org'
  gem 'sqlite3'
  gem 'rspec-expectations'

  gem 'activerecord', '~> 5.2', require: %w[active_record active_record/relation]
  gem 'activesupport', require: 'active_support/dependencies/autoload'
  gem 'ransack', github: 'activerecord-hackery/ransack', ref: '6434e3bedfbf4cc5d97d33c84d10682970a95083'
end

ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')

ActiveRecord::Schema.define do
  create_table :houses, force: true do |t|
  end

  create_table :locations, force: true do |t|
    t.references :locatable, polymorphic: true
  end
end

class House < ActiveRecord::Base
  has_one :location, as: :locatable
end

class Location < ActiveRecord::Base
  belongs_to :locatable, polymorphic: true
end

RSpec.describe Location, type: :model do
  it 'queries correctly' do
    expect(Location.ransack(locatable_of_House_type_id_eq: 123).result.to_sql).to eq(<<-SQL.squish)
      SELECT "locations".*
      FROM "locations"
      LEFT OUTER JOIN "houses" ON "houses"."id" = "locations"."locatable_id"
      WHERE "houses"."id" = 123
        AND "locations"."locatable_type" = 'House'
    SQL
end

Output:

Failures:

  1) Location queries correctly
     Failure/Error:
       expect(Location.ransack(locatable_of_House_type_id_eq: 123).result.to_sql).to eq(<<-SQL.squish)
         SELECT \"locations\".*
         FROM \"locations\"
         LEFT OUTER JOIN \"houses\" ON \"houses\".\"id\" = \"locations\".\"locatable_id\"
         WHERE \"houses\".\"id\" = 123
           AND \"locations\".\"locatable_type\" = 'House'
       SQL
     
       expected: "SELECT \"locations\".* FROM \"locations\" LEFT OUTER JOIN \"houses\" ON \"houses\".\"id\" = \"locati...ons\".\"locatable_id\" WHERE \"houses\".\"id\" = 123 AND \"locations\".\"locatable_type\" = 'House'"
            got: "SELECT \"locations\".* FROM \"locations\" LEFT OUTER JOIN \"houses\" ON \"houses\".\"id\" = \"locations\".\"locatable_id\" WHERE \"houses\".\"id\" = 123"
     
       (compared using ==)
@PhilCoggins
Copy link
Contributor

PhilCoggins commented May 7, 2020

This is a dupe of an issue I opened #1039.

I have tirelessly tried to get this issue addressed, and even tried to fix myself, but the outlook is grim. I have a monkey patch in that issue that will work up to Rails 5.2.3, but anything beyond that, including Rails 6 the monkey patch will not work.

Edit: Also see my comment in this PR that addresses the specific issue with incorrect JOIN being generated #1077 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants