You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 2require'bundler/inline'require'rspec/autorun'require'rspec/core/formatters/progress_formatter'require'rspec/core/profiler'# TODO: Define dependenciesgemfiletruedosource'https://rubygems.org'gem'sqlite3'gem'rspec-expectations'gem'activerecord','~> 5.2',require: %w[active_recordactive_record/relation]gem'activesupport',require: 'active_support/dependencies/autoload'gem'ransack',github: 'activerecord-hackery/ransack',ref: '6434e3bedfbf4cc5d97d33c84d10682970a95083'endActiveRecord::Base.establish_connection(adapter: 'sqlite3',database: ':memory:')ActiveRecord::Schema.definedocreate_table:houses,force: truedo |t|
endcreate_table:locations,force: truedo |t|
t.references:locatable,polymorphic: trueendendclassHouse < ActiveRecord::Basehas_one:location,as: :locatableendclassLocation < ActiveRecord::Basebelongs_to:locatable,polymorphic: trueendRSpec.describeLocation,type: :modeldoit'queries correctly'doexpect(Location.ransack(locatable_of_House_type_id_eq: 123).result.to_sql).toeq(<<-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' SQLend
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 ==)
The text was updated successfully, but these errors were encountered:
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)
Given the polymorphic example from the wiki:
The final query doesn't compare
locatable_type = 'House'
. This would gives false result, as theHouse
id can match IDs from other polymorphic types as well.Here is the full specs:
Output:
The text was updated successfully, but these errors were encountered: