Skip to content

Instantly share code, notes, and snippets.

@tomafro
Created June 14, 2011 05:52
Show Gist options
  • Save tomafro/1024399 to your computer and use it in GitHub Desktop.
Save tomafro/1024399 to your computer and use it in GitHub Desktop.
class Event < Node
end
class Account < ActiveRecord::Base
has_many :nodes
end
Account.first.nodes.create! :type => 'event'
class Message < Node
end
class Node < ActiveRecord::Base
belongs_to :account
def new(attributes = {}, *args, &block)
clazz = class_for(attributes.delete(:type))
if clazz == self
super(attributes, *args, &block)
else
clazz.new(attributes, *args, &block)
end
end
def class_for(t)
case t
when 'message' then Message
when 'event' then Event
else self
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment