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

Fix #41417 Handle Relations with having referencing aliased selects in #include? #41419

Commits on Feb 12, 2021

  1. Handle Relations with having clauses in #include?

    - skips optimised exists? query for relations that have a
      having clause
    
    Relations that have aliased select values AND a having clause that
    references an aliased select value would generate an error when
    #include? was called, due to an optimisation that would generate
    call #exists? on the relation instead, which effectively alters
    the select values of the query (and thus removes the aliased select
    values), but leaves the having clause intact. Because the having
    clause is then referencing an aliased column that is no longer
    present in the simplified query, an ActiveRecord::InvalidStatement
    error was raised.
    
    An sample query affected by this problem:
    
        Author.select('COUNT(*) as total_posts', 'authors.*')
              .joins(:posts)
              .group(:id)
              .having('total_posts > 2')
              .include?(Author.first)
    
    This change adds an addition check to the condition that skips the
    simplified #exists? query, which simply checks for the presence of
    a having clause.
    smartygus committed Feb 12, 2021
    Copy the full SHA
    644d694 View commit details
    Browse the repository at this point in the history