Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveRecord::Relation#merge merges other table's conditions having same attribute name

#5184

For example,

(Developer.joins(:company).where(:name => 'david') & Company.where(:name => '37signals')).to_sql

/ generates the following SQL.

SELECT "developers".* FROM "developers" INNER JOIN "companies" ON "companies"."id" = "developers"."company_id" WHERE ("companies"."name" = '37signals')

("developers"."name" = 'david') were overwritten because ActiveRecord::Relation#merge merges all Equality conditions with same attribute name.

Attached a patch to compare table_name as well when merging.

Reported by Akira Matsuda · July 23rd, 2010 @ 10:53 AM

State: resolved
Milestone: 3.0.2
Assigned to: Aaron Patterson Aaron Patterson
Importance: Low

Activity

  1. Akira Matsuda
    Akira Matsuda
    • Tag changed from activerecord rails3, merge relation, edge, merge, patch, relation, scope, tested to activerecord rails3, merge relation, bug, edge, merge, patch, relation, scope, tested

    Updated the patch so that it applies to the current Edge...

    August 22nd, 2010 @ 09:38 PM

  2. José Valim
    José Valim
    • Milestone cleared.
    • Assigned user set to Aaron Patterson
    • Importance changed from to Low

    August 23rd, 2010 @ 01:25 AM

  3. Aaron Patterson
    Aaron Patterson
    • State changed from new to incomplete

    Hi Akira!

    I tried this patch, but it makes the AR tests fail. Specifically a named scope test:

    $ ruby -w -I"lib:test:test/connections/native_sqlite3" test/cases/named_scope_test.rb -n test_chaining_should_use_latest_conditions_when_searching
    /Users/apatterson/git/arel/lib/arel/algebra/attributes/attribute.rb:70: warning: method redefined; discarding old root
    Using native SQLite3
    Loaded suite test/cases/named_scope_test
    Started
    F
    Finished in 0.063631 seconds.
    
      1) Failure:
    test_chaining_should_use_latest_conditions_when_searching(NamedScopeTest)
        [test/cases/named_scope_test.rb:352:in `test_chaining_should_use_latest_conditions_when_searching'
         /Users/apatterson/git/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:67:in `__send__'
         /Users/apatterson/git/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:67:in `run'
         /Users/apatterson/git/rails/activesupport/lib/active_support/callbacks.rb:418:in `_run_setup_callbacks'
         /Users/apatterson/git/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:65:in `run']:
    <[#<Post id: 4, author_id: 1, title: "sti comments", body: "hello", type: "Post", comments_count: 0, taggings_count: 0>]> expected but was
    <[]>.
    
    1 tests, 3 assertions, 1 failures, 0 errors
    

    Can you check the patch and make sure the AR tests pass?

    August 23rd, 2010 @ 01:47 AM

  4. Jeremy Kemper
  5. Jeff Kreeftmeijer
    Jeff Kreeftmeijer
    • Tag cleared.

    Automatic cleanup of spam.

    October 12th, 2010 @ 09:06 AM

  6. Aditya Sanghi
    Aditya Sanghi

    Automatic cleanup of spam.

    October 12th, 2010 @ 10:11 AM

  7. Aditya Sanghi
    Aditya Sanghi

    Automatic cleanup of spam.

    October 12th, 2010 @ 10:16 AM

  8. Ryan Bigg
    Ryan Bigg

    Automatic cleanup of spam.

    October 13th, 2010 @ 06:25 AM

  9. Jeremy Kemper
  10. Ryan Bigg
    Ryan Bigg

    Automatic cleanup of spam.

    October 19th, 2010 @ 08:35 AM

  11. Repository
  12. Repository