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 unscope when an eq node which has no arel attribute #38583

Merged
merged 1 commit into from
Feb 27, 2020

Commits on Feb 27, 2020

  1. Fix unscope when an eq node which has no arel attribute

    Current code expect an `eq` node has one arel attribute at least, but an
    `eq` node may have no arel attribute (e.g. `Arel.sql("...").eq(...)`).
    
    In that case `unscope` will raise `NoMethodError`:
    
    ```
    % bundle exec ruby -w -Itest test/cases/relations_test.rb -n test_unscope_with_arel_sql
    Using sqlite3
    Run options: -n test_unscope_with_arel_sql --seed 4477
    
    # Running:
    
    E
    
    Error:
    RelationTest#test_unscope_with_arel_sql:
    NoMethodError: undefined method `name' for #<Arel::Nodes::Quoted:0x00007f9938e55960>
        /Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/relation/where_clause.rb:157:in `block (2 levels) in except_predicates'
        /Users/kamipo/src/github.com/rails/rails/activerecord/lib/arel.rb:57:in `fetch_attribute'
        /Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/relation/where_clause.rb:157:in `block in except_predicates'
        /Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/relation/where_clause.rb:156:in `reject'
        /Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/relation/where_clause.rb:156:in `except_predicates'
        /Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/relation/where_clause.rb:31:in `except'
        /Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/relation/query_methods.rb:487:in `block (2 levels) in unscope!'
        /Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/relation/query_methods.rb:481:in `each'
        /Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/relation/query_methods.rb:481:in `block in unscope!'
        /Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/relation/query_methods.rb:471:in `each'
        /Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/relation/query_methods.rb:471:in `unscope!'
        /Users/kamipo/src/github.com/rails/rails/activerecord/lib/active_record/relation/query_methods.rb:464:in `unscope'
        test/cases/relations_test.rb:2062:in `test_unscope_with_arel_sql'
    ```
    
    We should check for both `value.left` and `value.right` those are arel
    attribute or not.
    kamipo committed Feb 27, 2020
    Configuration menu
    Copy the full SHA
    9698c1b View commit details
    Browse the repository at this point in the history