This project is archived and is in readonly mode.

#4502 ✓invalid

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

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

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.

Comments and changes to this ticket

  • Neeraj Singh

    Neeraj Singh May 12th, 2010 @ 05:02 AM

    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

    Car.lab is producing following sql

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

    darkhelmetlive May 12th, 2010 @ 05:28 AM

    I just cloned my repo ( 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
    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
  • Ryan Bigg

    Ryan Bigg May 13th, 2010 @ 11:52 PM

    • 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.

  • darkhelmetlive

    darkhelmetlive May 17th, 2010 @ 08:29 PM

    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.

  • Rohit Arondekar

    Rohit Arondekar June 15th, 2010 @ 01:48 PM

    • State changed from “incomplete” to “invalid”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>