forked from brunofrank/class-table-inheritance
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathREADME
105 lines (74 loc) · 2.28 KB
/
README
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
Versions
========
If you are using Rails 2.3.8 or other version < 3, you have to use the version 1.1.x of this plugin, for Rails 3 you need to use the version 1.2.x or master of this plugin.
ClassTableInheritance 1.2.0
===========================
This is an ActiveRecord plugin designed to allow
simple multiple table (class) inheritance.
This plugin was inspired by:
inherits_from plugin => http://github.com/rwl4/inherits_from and
Multiple Table Inheritance with ActiveRecord => http://mediumexposure.com/multiple-table-inheritance-active-record/
How to install
==============
gem install class-table-inheritance
Example Configuration
=====================
# Migrations
create_cti_table :product, :has_children => true do |t|
t.string :description
t.decimal :price
t.timestamps
end
create_cti_table :book, :has_parent => :product, :has_children => true do |t|
t.string :author
end
create_cti_table :novel, :has_parent => :book do |t|
t.string :genre
end
create_cti_table :encyclopedia, :has_parent => :book do |t|
t.string :editor
end
create_cti_table :videos, :has_parent => :product do |t|
t.string :year
t.string :genre
end
# Models
class Product < ActiveRecord::Base
has_children
end
class Book < ActiveRecord::Base
has_parent :product
end
class Novel < ActiveRecord::Base
has_parent :product
end
class Encyclopedia < ActiveRecord::Base
has_parent :product
end
class Video < ActiveRecord::Base
has_parent :product
end
Accessing children directly
===========================
book = Book.find(1)
book.name => "Agile Development with Rails"
book.author => "Dave Thomas"
book.price => 19.00
video = Video.find(2)
video.name => "Inseption"
video.year => "2010"
video.genre => "SCI-FI"
video.price => 22.00
book = Book.new
book.name = "Hamlet"
book.author = "Shakespeare, William"
book.price => 14.00
book.save
Accessing and searching children using parent
=============================================
product = Product.find(1) # This is a Book instance.
product.author
product = Product.find(2) # This is a Video instance.
product.genre
Copyright (c) 2010-2011 Derrick Parkhurst
Copyright (c) 2010 Bruno Cordeiro, released under the MIT license