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.

Using a where clause on a different table in a scope with a lambda overwrites itself

#4502

If I make a scope like this:

scope(:id_or_name, lambda { |f, v|
  where(f => v.match(/^\d+$/) ? { :id => v } : { :name => v })
})

and then use it like this:

u = User.scoped
u = u.id_or_name(:companies, '1') # like u.where(:accounts => { :id => '1' })
u = u.id_or_name(:divisions, 'engineering') # like u.where(:divisions => { :name => 'engineering' })

# results in "SELECT ... (`divisions`.`name` = 'engineering')"

the first calling gets 'overwritten' and the second one takes effect, when I would expect it to have both conditions in the SQL with an AND. If I do the where clauses myself without the scope, it works.

If the where is for the same table, it works

scope(:id_or_name_same_model, lambda { |v|
  where(v.match(/^\d+$/) ? { :id => v } : { :name => v })
})

u = User.scoped
u = u.id_or_name_same_model('1') # like u.where(:id => '1')
u = u.id_or_name_same_model('bob') # like u.where(:name => 'bob')

# results in "SELECT ..... (`users`.`id` = 1) AND (`users`.`name` = 'bob')"

More repro steps and whatnot can be seen in this gist. A full example project is here.

This occurred using the 3.0.0.beta3 gem and edge from github.

Reported by darkhelmetlive · April 29th, 2010 @ 10:38 PM

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Neeraj Singh
    Neeraj Singh

    I am not able to reproduce this error with rails edge.

    class Car < AR
     def self.lab
        c = Car.scoped
        c = c.id_or_name(:companies, '1') 
        c = c.id_or_name(:divisions, 'engineering') 
        puts c.to_sql
      end
    end
    

    Car.lab is producing following sql

    SELECT     "cars".* FROM       "cars" WHERE     ("companies"."id" = '1') AND ("divisions"."name" = 'engineering')
    

    May 12th, 2010 @ 05:02 AM

  2. darkhelmetlive
    darkhelmetlive

    I just cloned my repo (http://github.com/darkhelmet/scope_bug) and tried it again with edge, and it fails.

    If I run the console and do User.lab it prints the wrong SQL. Is there something I'm missing? Is there some subtle difference between what you are doing to repro and what I did?

    class User
      def self.lab
        u = User.scoped
        u = u.id_or_name(:companies, '1')
        u = u.id_or_name(:divisions, '2')
        puts u.to_sql
      end
    end
    
    user@shell: ./script/rails console
    Loading development environment (Rails 3.0.0.beta3)
    irb(main):001:0> User.lab
    SELECT     "users".* FROM       "users" WHERE     ("divisions"."id" = '2')
    => nil
    irb(main):002:0>
    

    May 12th, 2010 @ 05:28 AM

  3. Ryan Bigg
    Ryan Bigg
    • Tag changed from active_record, arel, rails3, scope to active_record, arel, bugmash, rails3, scope
    • State changed from new to incomplete

    Please provide a failing test case and patch for this issue.

    May 13th, 2010 @ 11:52 PM

  4. darkhelmetlive
    darkhelmetlive

    I tried to write a test case to replicate it, but it worked as it's supposed to. I'm either doing something wrong, or it was broken in beta3 and I didn't properly setup my Gemfile to use full edge for everything.

    This can be marked as invalid or whatever is appropriate.

    May 17th, 2010 @ 08:29 PM

  5. Rohit Arondekar
    Rohit Arondekar
    • State changed from incomplete to invalid

    June 15th, 2010 @ 01:48 PM