Created
October 20, 2012 00:11
-
-
Save kalleth/3921390 to your computer and use it in GitHub Desktop.
hmm, polymorphism?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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