public
Description: my random ruby scripts
Homepage:
Clone URL: git://github.com/kastner/ruby-junk.git
Click here to lend your support to: ruby-junk and make a donation at www.pledgie.com !
ruby-junk / ar_playground_polymorphic.rb
100755 52 lines (39 sloc) 1.061 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env ruby
 
%w|rubygems active_record irb|.each {|lib| require lib}
 
class Building < ActiveRecord::Base
belongs_to :owner, :polymorphic => true
end
 
class Person < ActiveRecord::Base
has_many :buildings, :as => :owner
end
 
class Company < ActiveRecord::Base
has_many :buildings, :as => :owner
end
 
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => ":memory:"
)
 
ActiveRecord::Schema.define do
create_table :buildings do |t|
t.string :address
t.references :owner, :polymorphic => true
end
create_table :people do |t|
t.string :name
t.integer :age
end
create_table :companies do |t|
    t.string :name
    t.string :tax_id
  end
end
 
apple = Company.create(:name => "Apple", :tax_id => "123-abc")
steve = Person.create(:name => "Steve Jobs", :age => 100)
 
b1 = Building.create(:address => "1 Infinate Loop")
b2 = Building.create(:address => "123 Fake st.")
 
b1.owner = apple
b1.save
 
b2.owner = steve
b2.save
 
IRB.start if __FILE__ == $0