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.

STI ignored with scopes

#4507

Using Rails3 edge, I have created an Event model with the following scope:

scope :active, where([ "start > ?", 2.hours.ago ])

Then I extend that model to use STI with two further models: Conference and QaSession.

Now when I run the following it works:

ruby-1.9.2-head > Conference.all
  SQL (0.4ms)  SHOW TABLES
  Conference Load (0.4ms)  SELECT `events`.* FROM `events` WHERE (`events`.`type` = 'Conference')

But when I use the 'active' scope:

ruby-1.9.2-head > Conference.active
  Event Load (0.5ms)  SELECT `events`.* FROM `events` WHERE (start > '2010-04-29 23:52:41')

As you can see when calling the scope method, the STI part of the SQL query is not included at all. The query should be:

SELECT `events`.* FROM `events` WHERE (start > '2010-04-29 23:52:41') AND (`events`.`type` = 'Conference')

I am pretty sure that this is a bug, as I don't think I am setting and calling the scope incorrectly. thx.

Reported by joel (at developwithstyle) · April 30th, 2010 @ 02:58 AM

State: resolved
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. joel (at developwithstyle)
    joel (at developwithstyle)

    Ok, so after further investigation, this is ony happenning when the scope is defined within the parent model (Event).

    April 30th, 2010 @ 09:10 AM

  2. Neeraj Singh
    Neeraj Singh
    • Tag changed from scope, sti to patch, rails3, scope, sti

    Attached is a patch against Rails3 edge along with test.

    Please see this gist if you are interested in why the solution is the way it is.

    May 3rd, 2010 @ 05:28 PM

  3. José Valim
    José Valim
    • Milestone cleared.
    • State changed from new to open
    • Assigned user set to José Valim

    May 3rd, 2010 @ 05:38 PM

  4. Neeraj Singh
    Neeraj Singh

    Attached is an improvised patch based on feedback by Valim.

    May 3rd, 2010 @ 05:49 PM

  5. Repository
    Repository
    • State changed from open to resolved

    (from [58adc6737126242d189274d83c50bcade098800a]) STI should not ignore type condition while applying scopes from parent class scopes

    [#4507 STI ignored with scopes state:resolved]

    Signed-off-by: José Valim jose.valim@gmail.com
    http://github.com/rails/rails/commit/58adc6737126242d189274d83c50bc...

    May 15th, 2010 @ 08:51 AM

  6. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to Low

    October 15th, 2010 @ 11:01 PM