Skip to content

Rails/WhereEquals autocorrect changes query generation behavior #403

Closed
@yitznewton

Description

On updating 2.8.1 to 2.9.0, running autocorrect on a particular piece of code results in Rails incorrectly inferring the table of a column (vendors.staged), and generating a different Postgres query for the wrong table. In this case the code itself can be fixed after autocorrect by specifying the table name.

Active Record code before:

  def self.include_staged(staged)
    s = all.joins(:vendor)
    s = s.where("staged = ?", false) unless staged
    s
  end

SQL before:

SELECT  DISTINCT "vendor_updates"."id", vendors.name AS alias_0
FROM "vendor_updates"
INNER JOIN "public"."vendors" ON "public"."vendors"."id" = "vendor_updates"."vendor_id"
LEFT OUTER JOIN "pricing_zones_vendors" ON "pricing_zones_vendors"."vendor_id" = "public"."vendors"."id"
LEFT OUTER JOIN "zones" ON "zones"."id" = "pricing_zones_vendors"."pricing_zone_id"
LEFT OUTER JOIN "data_files" ON "data_files"."id" = "vendor_updates"."parsed_file_id"
WHERE "vendor_updates"."month" = $1 AND (staged = 'f') 
ORDER BY vendors.name asc LIMIT 100 OFFSET 0

Active Record after:

  def self.include_staged(staged)
    s = all.joins(:vendor)
    s = s.where(staged: false) unless staged
    s
  end

SQL after:

SELECT  DISTINCT "vendor_updates"."id", vendors.name AS alias_0
FROM "vendor_updates"
INNER JOIN "public"."vendors" ON "public"."vendors"."id" = "vendor_updates"."vendor_id"
LEFT OUTER JOIN "pricing_zones_vendors" ON "pricing_zones_vendors"."vendor_id" = "public"."vendors"."id"
LEFT OUTER JOIN "zones" ON "zones"."id" = "pricing_zones_vendors"."pricing_zone_id"
LEFT OUTER JOIN "data_files" ON "data_files"."id" = "vendor_updates"."parsed_file_id"
WHERE "vendor_updates"."month" = $1 AND "vendor_updates"."staged" = 'f' 
ORDER BY vendors.name asc LIMIT 100 OFFSET 0

RuboCop version

Before

$ rubocop -V
1.5.2 (using Parser 2.7.2.0, rubocop-ast 1.3.0, running on ruby 2.6.6 x86_64-linux)
  - rubocop-performance 1.9.1
  - rubocop-rails 2.8.1

After

$ rubocop -V
1.5.2 (using Parser 2.7.2.0, rubocop-ast 1.3.0, running on ruby 2.6.6 x86_64-linux)
  - rubocop-performance 1.9.1
  - rubocop-rails 2.9.0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions