Skip to content

Instantly share code, notes, and snippets.

@linjunpop
Created September 26, 2012 07:39
Show Gist options
  • Select an option

  • Save linjunpop/3786614 to your computer and use it in GitHub Desktop.

Select an option

Save linjunpop/3786614 to your computer and use it in GitHub Desktop.
Rails 2 STI changes behavior depending on environment

Copied from: https://rails.lighthouseapp.com/projects/8994/tickets/2389

The problem occurs(for me) when cache_classes is set to false

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' ) )

Solution:

class ProjectFeedEvent < FeedEvent
  def self.subclasses
    [ProjectAddedEvent]
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment