Prevent missing type / NameError in polymorphic ActiveRecord associations

ActiveRecord loads the xxx_type in your model, making it blow up when doing includes / using the belongs_to on a missing type.
So we make it un-missing.

Usage

class Waldo < MissingType
end

Code

class MissingType < ActiveRecord::Base
  default_scope :conditions => "1 = 2", :limit => 0

  self.table_name = "schema_migrations"

  def self.primary_key
    "version"
  end

  def readonly?
    true
  end
end

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s