-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME
61 lines (40 loc) · 1.47 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
InheritableAttributes
=====================
Useful if model attributes are to be inherited from a parent object if blank.
Example
=======
class Company < ActiveRecord::Base
# company has an attribute "address"
has_many :divisions
end
class Division < ActiveRecord::Base
# division has an attribute "address"
belongs_to :company
inherit_attribute :address, :from => :company
end
class Department < ActiveRecord::Base
# department has an attribute "address"
belongs_to :division
inherit_attribute :address, :from => :division
end
d = Department.first
d.address #=> returns d.address, unless that value is blank, in which case
it returns d.division.address, unless that value is blank,
in which case it returns d.division.company.address
You can also specify more than one attribute to inherit:
class Department
# department has an attribute "address"
belongs_to :division
inherit_attributes [:address, :telephone], :from => :division
end
If the attribute has a different name in the parent, you can use the :as
option to specify it:
class Depot < ActiveRecord::Base
belongs_to :company
inherit_attribute :postal_address, :from => :company, :as => :address
end
d = Depot.first
d.postal_address #=> returns d.postal_address, unless that value is blank,
in which case it returns d.company.address
Copyright (c) 2008 Jon Evans, Springy Web (UK) Ltd. Released under the MIT license