mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fixed readme for ActiveModel::Dirty
This commit is contained in:
parent
d1bedd182b
commit
db274a02ed
1 changed files with 22 additions and 4 deletions
|
@ -41,20 +41,38 @@ You can include functionality from the following modules:
|
||||||
|
|
||||||
* Tracking changes in your object
|
* Tracking changes in your object
|
||||||
|
|
||||||
class MyClass
|
class Person
|
||||||
include ActiveModel::Dirty
|
include ActiveModel::Dirty
|
||||||
|
|
||||||
|
define_attribute_methods [:name]
|
||||||
|
|
||||||
|
def name
|
||||||
|
@name
|
||||||
|
end
|
||||||
|
|
||||||
|
def name=(val)
|
||||||
|
name_will_change!
|
||||||
|
@name = val
|
||||||
|
end
|
||||||
|
|
||||||
|
def save
|
||||||
|
@previously_changed = changes
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
...provides all the value tracking features implemented by ActiveRecord
|
...provides all the value tracking features implemented by ActiveRecord
|
||||||
|
|
||||||
person.name # => 'bill'
|
person = Person.new
|
||||||
|
person.name # => nil
|
||||||
person.changed? # => false
|
person.changed? # => false
|
||||||
person.name = 'bob'
|
person.name = 'bob'
|
||||||
person.changed? # => true
|
person.changed? # => true
|
||||||
person.changed # => ['name']
|
person.changed # => ['name']
|
||||||
person.changes # => { 'name' => ['bill', 'bob'] }
|
person.changes # => { 'name' => [nil, 'bob'] }
|
||||||
person.name = 'robert'
|
person.name = 'robert'
|
||||||
person.save
|
person.save
|
||||||
person.previous_changes # => {'name' => ['bob, 'robert']}
|
person.previous_changes # => {'name' => ['bob, 'robert']}
|
||||||
|
|
||||||
|
{Learn more}[link:classes/ActiveModel/Dirty.html]
|
||||||
|
|
Loading…
Reference in a new issue