The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
STI and Abstract Classes Driving You Nuts?

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ryan Davis

Posts: 651
Nickname: zenspider
Registered: Oct, 2004

Ryan Davis is a ruby nerd.
STI and Abstract Classes Driving You Nuts? Posted: Jan 5, 2007 4:53 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Ryan Davis.
Original Post: STI and Abstract Classes Driving You Nuts?
Feed Title: Polishing Ruby
Feed URL: http://blog.zenspider.com/index.rdf
Feed Description: Musings on Ruby and the Ruby Community...
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Ryan Davis
Latest Posts From Polishing Ruby

Advertisement

If you happen to be hit by Ticket #5704: Bug in ActiveRecord::Base when mixing STI and abstract models, and you're seeing failures to find STI instances intermittently in development mode, try the following:

class MyAbstractModel < ActiveRecard::Base
  self.abstract_class = false
  # ...
end

-----

class MySTIModel < MyAbstractModel
  TYPES = %w(sub1 sub2 sub3)
  def self.inherited(cls)
    super
    raise "not in #{self.class}::TYPES: #{cls}" unless TYPES.include? cls.name.downcase
  end
end

MySTIModel::TYPES.each do |f|
  require_dependency f
end

-----

class Sub1 < MySTIModel; end
class Sub2 < MySTIModel; end
class Sub3 < MySTIModel; end

It is a stupid bug and I can't do much about it, but the above workaround seems to be a nice tradeoff between horrible hack and maintainability. By pushing the list of subclasses up to the TYPES array, you keep it visible and near the top of the file. By having the inherited hook, you keep slipups in maintainability as visible as possible.

Read: STI and Abstract Classes Driving You Nuts?

Topic: 2006 in Review Previous Topic   Next Topic Topic: Google Code Jam ahora para Latinoam��rica

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use