Skip to content

Instantly share code, notes, and snippets.

@kalleth
Created October 20, 2012 00:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kalleth/3921390 to your computer and use it in GitHub Desktop.
Save kalleth/3921390 to your computer and use it in GitHub Desktop.
hmm, polymorphism?
class DataCenter < ActiveRecord::Base
# has configuration_type, configuration_id attributes
def config
configuration_type.constantize.find(configuration_id)
end
def config=(config)
configuration_type = config.class.name
configuration_id = config.id
end
end
class AwsConfiguration < ActiveRecord::Base
end
class LinodeConfiguration < ActiveRecord::Base
end
#<DataCentre id: 1, configuration_type: 'AwsConfiguration', configuration_id: '5'>
@dc.config # => #<AwsConfiguration id: 5>
#<DataCentre id: 2, configuration_type: 'LinodeConfiguration', configuration_id: '21'>
@dc.config # => #<LinodeConfiguration id: 21>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment