Skip to content

Commit

Permalink
docs, make association autosave: true examples runnable. Closes #14700
Browse files Browse the repository at this point in the history


[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.
  • Loading branch information
senny committed Apr 11, 2014
1 parent 74d920d commit daea4cf
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions activerecord/lib/active_record/autosave_association.rb
Expand Up @@ -35,7 +35,7 @@ module ActiveRecord
# #
# === One-to-one Example # === One-to-one Example
# #
# class Post # class Post < ActiveRecord::Base
# has_one :author, autosave: true # has_one :author, autosave: true
# end # end
# #
Expand Down Expand Up @@ -76,7 +76,7 @@ module ActiveRecord
# #
# When <tt>:autosave</tt> is not declared new children are saved when their parent is saved: # 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 # has_many :comments # :autosave option is not declared
# end # end
# #
Expand All @@ -95,20 +95,23 @@ module ActiveRecord
# When <tt>:autosave</tt> is true all children are saved, no matter whether they # When <tt>:autosave</tt> is true all children are saved, no matter whether they
# are new records or not: # are new records or not:
# #
# class Post # class Post < ActiveRecord::Base
# has_many :comments, autosave: true # has_many :comments, autosave: true
# end # end
# #
# post = Post.create(title: 'ruby rocks') # post = Post.create(title: 'ruby rocks')
# post.comments.create(body: 'hello world') # post.comments.create(body: 'hello world')
# post.comments[0].body = 'hi everyone' # 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 # Destroying one of the associated models as part of the parent's save action
# is as simple as marking it for destruction: # is as simple as marking it for destruction:
# #
# post.comments.last.mark_for_destruction # post.comments # => [#<Comment id: 1, ...>, #<Comment id: 2, ...]>
# post.comments.last.marked_for_destruction? # => true # post.comments[1].mark_for_destruction
# post.comments[1].marked_for_destruction? # => true
# post.comments.length # => 2 # post.comments.length # => 2
# #
# Note that the model is _not_ yet removed from the database: # Note that the model is _not_ yet removed from the database:
Expand Down

0 comments on commit daea4cf

Please sign in to comment.