mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
docs, make association autosave: true
examples runnable. Closes #14700
[ci skip] The examples are written in a way you expect them to be executable. However one snippet assumed there to be two comments when only one was created above. The defined models did not extend `ActiveRecord::Base` The example used `comments.last.mark_for_destruction`. This does no longer load the whole collection but just the last record. It is then refetcht on subsequent calls to `last`. This breaks the example.
This commit is contained in:
parent
a7a180bd81
commit
856ffbe705
1 changed files with 9 additions and 6 deletions
|
@ -35,7 +35,7 @@ module ActiveRecord
|
|||
#
|
||||
# === One-to-one Example
|
||||
#
|
||||
# class Post
|
||||
# class Post < ActiveRecord::Base
|
||||
# has_one :author, autosave: true
|
||||
# end
|
||||
#
|
||||
|
@ -76,7 +76,7 @@ module ActiveRecord
|
|||
#
|
||||
# When <tt>:autosave</tt> is not declared new children are saved when their parent is saved:
|
||||
#
|
||||
# class Post
|
||||
# class Post < ActiveRecord::Base
|
||||
# has_many :comments # :autosave option is not declared
|
||||
# end
|
||||
#
|
||||
|
@ -95,20 +95,23 @@ module ActiveRecord
|
|||
# When <tt>:autosave</tt> is true all children are saved, no matter whether they
|
||||
# are new records or not:
|
||||
#
|
||||
# class Post
|
||||
# class Post < ActiveRecord::Base
|
||||
# has_many :comments, autosave: true
|
||||
# end
|
||||
#
|
||||
# post = Post.create(title: 'ruby rocks')
|
||||
# post.comments.create(body: 'hello world')
|
||||
# post.comments[0].body = 'hi everyone'
|
||||
# post.save # => saves both post and comment, with 'hi everyone' as body
|
||||
# post.comments.build(body: "good morning.")
|
||||
# post.title += "!"
|
||||
# post.save # => saves both post and comments.
|
||||
#
|
||||
# Destroying one of the associated models as part of the parent's save action
|
||||
# is as simple as marking it for destruction:
|
||||
#
|
||||
# post.comments.last.mark_for_destruction
|
||||
# post.comments.last.marked_for_destruction? # => true
|
||||
# post.comments # => [#<Comment id: 1, ...>, #<Comment id: 2, ...]>
|
||||
# post.comments[1].mark_for_destruction
|
||||
# post.comments[1].marked_for_destruction? # => true
|
||||
# post.comments.length # => 2
|
||||
#
|
||||
# Note that the model is _not_ yet removed from the database:
|
||||
|
|
Loading…
Reference in a new issue