Created
May 13, 2009 14:16
-
-
Save kastner/111041 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
%w|rubygems active_record irb|.each {|lib| require lib} | |
ActiveSupport::Inflector.inflections.singular("toyota", "toyota") | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Base.establish_connection( | |
:adapter => "sqlite3", | |
:database => ":memory:" | |
) | |
ActiveRecord::Schema.define do | |
create_table :cars do |t| | |
t.string :name | |
end | |
create_table :car_to_wheel_table_map, :id => false do |t| | |
t.integer :car_id | |
t.string :wheel_table | |
end | |
create_table :wheels_for_fords do |t| | |
t.integer :car_id | |
t.string :color | |
end | |
create_table :wheels_for_toyotas do |t| | |
t.integer :car_id | |
t.string :color | |
end | |
end | |
class Wheel < ActiveRecord::Base | |
set_table_name nil | |
belongs_to :car | |
end | |
class CarWheelMap < ActiveRecord::Base | |
set_table_name "car_to_wheel_table_map" | |
belongs_to :car | |
end | |
class Car < ActiveRecord::Base | |
has_one :car_wheel_map | |
delegate :wheel_table, :to => :car_wheel_map | |
def wheels | |
@wheels ||= begin | |
the_klass = "#{wheel_table.classify}Wheel" | |
eval <<-END | |
class #{the_klass} < ActiveRecord::Base | |
set_table_name "wheels_for_#{wheel_table.pluralize}" | |
belongs_to :car | |
end | |
END | |
self.class.send(:has_many, "#{wheel_table}_wheels") | |
send "#{wheel_table}_wheels" | |
end | |
end | |
end | |
rav4 = Car.create(:name => "Rav4") | |
rav4.create_car_wheel_map(:wheel_table => "toyota") | |
fiesta = Car.create(:name => "Fiesta") | |
fiesta.create_car_wheel_map(:wheel_table => "ford") | |
rav4.wheels.create(:color => "red") | |
fiesta.wheels.create(:color => "green") | |
# IRB.start if __FILE__ == $0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment