回答編集履歴
1
追記
test
CHANGED
@@ -35,3 +35,119 @@
|
|
35
35
|
end
|
36
36
|
|
37
37
|
```
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
ソースコードから一部抜粋
|
42
|
+
|
43
|
+
```rb
|
44
|
+
|
45
|
+
# Change the name:
|
46
|
+
|
47
|
+
#
|
48
|
+
|
49
|
+
# person.name = 'Bob'
|
50
|
+
|
51
|
+
# person.changed? # => true
|
52
|
+
|
53
|
+
# person.name_changed? # => true
|
54
|
+
|
55
|
+
# person.name_changed?(from: nil, to: "Bob") # => true
|
56
|
+
|
57
|
+
# person.name_was # => nil
|
58
|
+
|
59
|
+
# person.name_change # => [nil, "Bob"]
|
60
|
+
|
61
|
+
# person.name = 'Bill'
|
62
|
+
|
63
|
+
# person.name_change # => [nil, "Bill"]
|
64
|
+
|
65
|
+
#
|
66
|
+
|
67
|
+
# Save the changes:
|
68
|
+
|
69
|
+
#
|
70
|
+
|
71
|
+
# person.save
|
72
|
+
|
73
|
+
# person.changed? # => false
|
74
|
+
|
75
|
+
# person.name_changed? # => false
|
76
|
+
|
77
|
+
#
|
78
|
+
|
79
|
+
# Reset the changes:
|
80
|
+
|
81
|
+
#
|
82
|
+
|
83
|
+
# person.previous_changes # => {"name" => [nil, "Bill"]}
|
84
|
+
|
85
|
+
# person.name_previously_changed? # => true
|
86
|
+
|
87
|
+
# person.name_previous_change # => [nil, "Bill"]
|
88
|
+
|
89
|
+
# person.reload!
|
90
|
+
|
91
|
+
# person.previous_changes # => {}
|
92
|
+
|
93
|
+
#
|
94
|
+
|
95
|
+
# Rollback the changes:
|
96
|
+
|
97
|
+
#
|
98
|
+
|
99
|
+
# person.name = "Uncle Bob"
|
100
|
+
|
101
|
+
# person.rollback!
|
102
|
+
|
103
|
+
# person.name # => "Bill"
|
104
|
+
|
105
|
+
# person.name_changed? # => false
|
106
|
+
|
107
|
+
#
|
108
|
+
|
109
|
+
# Assigning the same value leaves the attribute unchanged:
|
110
|
+
|
111
|
+
#
|
112
|
+
|
113
|
+
# person.name = 'Bill'
|
114
|
+
|
115
|
+
# person.name_changed? # => false
|
116
|
+
|
117
|
+
# person.name_change # => nil
|
118
|
+
|
119
|
+
#
|
120
|
+
|
121
|
+
# Which attributes have changed?
|
122
|
+
|
123
|
+
#
|
124
|
+
|
125
|
+
# person.name = 'Bob'
|
126
|
+
|
127
|
+
# person.changed # => ["name"]
|
128
|
+
|
129
|
+
# person.changes # => {"name" => ["Bill", "Bob"]}
|
130
|
+
|
131
|
+
#
|
132
|
+
|
133
|
+
# If an attribute is modified in-place then make use of
|
134
|
+
|
135
|
+
# <tt>[attribute_name]_will_change!</tt> to mark that the attribute is changing.
|
136
|
+
|
137
|
+
# Otherwise \Active \Model can't track changes to in-place attributes. Note
|
138
|
+
|
139
|
+
# that Active Record can detect in-place modifications automatically. You do
|
140
|
+
|
141
|
+
# not need to call <tt>[attribute_name]_will_change!</tt> on Active Record models.
|
142
|
+
|
143
|
+
#
|
144
|
+
|
145
|
+
# person.name_will_change!
|
146
|
+
|
147
|
+
# person.name_change # => ["Bill", "Bill"]
|
148
|
+
|
149
|
+
# person.name << 'y'
|
150
|
+
|
151
|
+
# person.name_change # => ["Bill", "Billy"]
|
152
|
+
|
153
|
+
```
|