回答編集履歴
1
追記
answer
CHANGED
@@ -16,4 +16,62 @@
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
19
|
+
```
|
20
|
+
|
21
|
+
ソースコードから一部抜粋
|
22
|
+
```rb
|
23
|
+
# Change the name:
|
24
|
+
#
|
25
|
+
# person.name = 'Bob'
|
26
|
+
# person.changed? # => true
|
27
|
+
# person.name_changed? # => true
|
28
|
+
# person.name_changed?(from: nil, to: "Bob") # => true
|
29
|
+
# person.name_was # => nil
|
30
|
+
# person.name_change # => [nil, "Bob"]
|
31
|
+
# person.name = 'Bill'
|
32
|
+
# person.name_change # => [nil, "Bill"]
|
33
|
+
#
|
34
|
+
# Save the changes:
|
35
|
+
#
|
36
|
+
# person.save
|
37
|
+
# person.changed? # => false
|
38
|
+
# person.name_changed? # => false
|
39
|
+
#
|
40
|
+
# Reset the changes:
|
41
|
+
#
|
42
|
+
# person.previous_changes # => {"name" => [nil, "Bill"]}
|
43
|
+
# person.name_previously_changed? # => true
|
44
|
+
# person.name_previous_change # => [nil, "Bill"]
|
45
|
+
# person.reload!
|
46
|
+
# person.previous_changes # => {}
|
47
|
+
#
|
48
|
+
# Rollback the changes:
|
49
|
+
#
|
50
|
+
# person.name = "Uncle Bob"
|
51
|
+
# person.rollback!
|
52
|
+
# person.name # => "Bill"
|
53
|
+
# person.name_changed? # => false
|
54
|
+
#
|
55
|
+
# Assigning the same value leaves the attribute unchanged:
|
56
|
+
#
|
57
|
+
# person.name = 'Bill'
|
58
|
+
# person.name_changed? # => false
|
59
|
+
# person.name_change # => nil
|
60
|
+
#
|
61
|
+
# Which attributes have changed?
|
62
|
+
#
|
63
|
+
# person.name = 'Bob'
|
64
|
+
# person.changed # => ["name"]
|
65
|
+
# person.changes # => {"name" => ["Bill", "Bob"]}
|
66
|
+
#
|
67
|
+
# If an attribute is modified in-place then make use of
|
68
|
+
# <tt>[attribute_name]_will_change!</tt> to mark that the attribute is changing.
|
69
|
+
# Otherwise \Active \Model can't track changes to in-place attributes. Note
|
70
|
+
# that Active Record can detect in-place modifications automatically. You do
|
71
|
+
# not need to call <tt>[attribute_name]_will_change!</tt> on Active Record models.
|
72
|
+
#
|
73
|
+
# person.name_will_change!
|
74
|
+
# person.name_change # => ["Bill", "Bill"]
|
75
|
+
# person.name << 'y'
|
76
|
+
# person.name_change # => ["Bill", "Billy"]
|
19
77
|
```
|