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 changes behavior depending on environment

#2389

This is a follow up to http://dev.rubyonrails.org/ticke...

and

https://rails.lighthouseapp.com/...

The problem occurs(for me) when cache_classes is set to false. For a pretty version of the code for the test case: http://pastie.org/433401


class FeedEvent < ActiveRecord::Base
end

class ProjectFeedEvent < FeedEvent
end

class ProjectAddedEvent < ProjectFeedEvent
end


#with config.cache_classes = false

#First web request
ProjectFeedEvent.find(:all) 
  => SELECT * FROM `feed_events` WHERE ( (`feed_events`.`type` = 'ProjectFeedEvent' OR `feed_events`.`type` =      'ProjectAddedEvent' ) )

#second web request
ProjectFeedEvent.find(:all) 
  => SELECT * FROM `feed_events` WHERE ( (`feed_events`.`type` = 'ProjectFeedEvent' ) )

Reported by Pete P. · April 1st, 2009 @ 04:30 AM

State: wontfix
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. leh
    leh

    I'm experiencing the same behavior.

    April 1st, 2009 @ 01:06 PM

  2. Pete P.
    Pete P.

    As a note, FeedEvent has a polymorphic association

    
      belongs_to :source, :polymorphic => true
    

    April 1st, 2009 @ 06:30 PM

  3. Pete P.
    Pete P.

    Ok, I've found the offending method(sort of)...

    Its ActiveRecord::Base#reset_subclasses

    I've attached a simple test case which details it. A couple of key points...if you execute all the preparatory statements in the test case(eg, migration and model creation) in a console, the test case will pass. However, if you create appropriate files for the models and migrations, and then load the console and run the subsequent test code, the test case will fail! The plot thickens...

    April 1st, 2009 @ 07:12 PM

  4. Pete P.
    Pete P.

    The work around(ugh...)

    You must explicitly name subclasses in the parent class

    
      #for the example above
      class ProjectFeedEvent < FeedEvent
        def self.subclasses
          [ProjectAddedEvent]
        end
      end
    

    April 1st, 2009 @ 07:44 PM

  5. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from 2.3.2, cache_classes, inheritance, single, table to 2.3.2, bugmash, cache_classes, inheritance, single, table

    August 6th, 2009 @ 12:55 PM

  6. Peer Allan
    Peer Allan

    verified that the following the steps in the console did duplicate the problem in both master and 2-3-stable. Attempted to verify using the browser when running script/server in development, but was unable to.

    Not sure what is going on here. If the classes are all defined in the same model file then the above behaviour occurs. When I separated the model files the SubTest.all used the same SQL before and after the reload of the subclasses.

    August 10th, 2009 @ 04:24 AM

  7. Tarmo Tänav
    Tarmo Tänav

    This is a very old and somewhat known issue [1] due to the development environment not explicitly loading all classes (unlike the production environment) and thus only being aware of the classes that have been referenced during the current request in some way.

    A workaround for this is to make the base class model file cause the subclasses to be loaded aswell by listing them at the bottom of the file:

    require_association 'subclass1'
    require_association 'subclass2'
    

    This way when the base class is referenced in development mode it also loads all the listed subclasses (which could then load their own subclasses as well should there be a deeper hierarchy).

    I can't see any simple fixes to this issue that don't significantly alter the way the development environment works.

    [1] http://dev.rubyonrails.org/ticket/4730

    August 26th, 2009 @ 03:04 PM

  8. John Pignata
    John Pignata

    -1 - I can't reproduce this using 2.3.4

    September 27th, 2009 @ 05:23 AM

  9. Akira Matsuda
    Akira Matsuda

    -1 couldn't reproduce on 2.3.4

    September 27th, 2009 @ 07:31 AM

  10. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from 2.3.2, bugmash, cache_classes, inheritance, single, table to 2.3.2, cache_classes, inheritance, single, table
    • State changed from new to stale

    Staling because appears to be no longer repro, and no patch available. Please reopen if you have additional info or ideas.

    September 27th, 2009 @ 11:41 AM

  11. Marcin 'swistak' Raczkowski
    Marcin 'swistak' Raczkowski
    • Tag changed from 2.3.2, cache_classes, inheritance, single, table to 2.3.2, cache_classes, inheritance, single, table

    +1, still happens here on 2.3.4 in dev mode, with class preloading turned off.

    Also it's impossible to override the default behaviour with conditions, since it always adds AND table_name.type="Somthing".

    October 26th, 2009 @ 10:57 AM

  12. Jonathan Dance
    Jonathan Dance
    • Tag changed from 2.3.2, cache_classes, inheritance, single, table to 2.3.5, cache_classes, sti
    1. I've reproduced this bug in 2.3.5. It has to do with autoloading, so the classes have to be in separate files. I am guessing those trying to reproduce in the past ran the test case in a single file. The workaround does work (defining self.subclasses).

    April 1st, 2010 @ 02:40 AM

  13. Paul Schreiber
    Paul Schreiber
    • Importance changed from to

    I reproduced this problem on 2.3.10 and wrote it up on StackOverflow.

    Adding require_association didn't work for me, but explicitly listing the subclasses (per Pete P.) did.

    November 9th, 2010 @ 10:20 PM

  14. Neeraj Singh
    Neeraj Singh
    • State changed from stale to wontfix

    This is a well documented issue. I will add a link to the document when I find that.

    November 10th, 2010 @ 01:59 PM