Skip to content

Commit

Permalink
adding more documentation for autosave option
Browse files Browse the repository at this point in the history
  • Loading branch information
Neeraj Singh authored and fxn committed Aug 12, 2010
1 parent 30ea923 commit 06af291
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
3 changes: 2 additions & 1 deletion activerecord/lib/active_record/associations.rb
Expand Up @@ -311,7 +311,8 @@ def association_instance_set(name, association)
# You can set the :autosave option on a <tt>has_one</tt>, <tt>belongs_to</tt>, # You can set the :autosave option on a <tt>has_one</tt>, <tt>belongs_to</tt>,
# <tt>has_many</tt>, or <tt>has_and_belongs_to_many</tt> association. Setting it # <tt>has_many</tt>, or <tt>has_and_belongs_to_many</tt> association. Setting it
# to +true+ will _always_ save the members, whereas setting it to +false+ will # to +true+ will _always_ save the members, whereas setting it to +false+ will
# _never_ save the members. # _never_ save the members. More details about :autosave option is available at
# autosave_association.rb .
# #
# === One-to-one associations # === One-to-one associations
# #
Expand Down
50 changes: 37 additions & 13 deletions activerecord/lib/active_record/autosave_association.rb
Expand Up @@ -18,6 +18,10 @@ module ActiveRecord
# Note that it also means that associations marked for destruction won't # Note that it also means that associations marked for destruction won't
# be destroyed directly. They will however still be marked for destruction. # be destroyed directly. They will however still be marked for destruction.
# #
# Do note that <tt>:autosave => false</tt> is not same as not declaring <tt>:autosave</tt>
# option. When <tt>:autosave</tt> option is not declared then it works in
# theoreticall <tt>:new_only</tt> mode. Look at has_many example discused below for details.
#
# === One-to-one Example # === One-to-one Example
# #
# class Post # class Post
Expand Down Expand Up @@ -57,27 +61,45 @@ module ActiveRecord
# #
# === One-to-many Example # === One-to-many Example
# #
# When <tt>autosave</tt> is not declared then also children will get saved when parent is saved
# in certain conditions.
#
# Consider a Post model with many Comments: # Consider a Post model with many Comments:
# #
# class Post # class Post
# has_many :comments, :autosave => true # has_many :comments # :autosave option is no declared
# end # end
# #
# Saving changes to the parent and its associated model can now be performed # post = Post.new(:title => 'ruby rocks')
# automatically _and_ atomically: # post.comments.build(:body => 'hello world')
# post.save #=> will save both post and comment
# #
# post = Post.find(1) # post = Post.create(:title => 'ruby rocks')
# post.title # => "The current global position of migrating ducks" # post.comments.build(:body => 'hello world')
# post.comments.first.body # => "Wow, awesome info thanks!" # post.save #=> will save both post and comment
# post.comments.last.body # => "Actually, your article should be named differently."
# #
# post.title = "On the migration of ducks" # post = Post.create(:title => 'ruby rocks')
# post.comments.last.body = "Actually, your article should be named differently. [UPDATED]: You are right, thanks." # post.comments.create(:body => 'hello world')
# post.save #=> will save both post and comment
# #
# post.save # post = Post.create(:title => 'ruby rocks')
# post.reload # post.comments.build(:body => 'hello world')
# post.title # => "On the migration of ducks" # post.comments[0].body = 'hi everyone'
# post.comments.last.body # => "Actually, your article should be named differently. [UPDATED]: You are right, thanks." # post.save #=> will save both post and comment and comment will have 'hi everyone'
#
# In the above cases even without <tt>autosave</tt> option children got updated.
#
# class Post
# has_many :comments, :autosave => true
# end
#
# <tt>:autosave</tt> declaration is required if an attempt is made to change an existing
# associatin in memory.

This comment has been minimized.

Copy link
@vesan

vesan Aug 24, 2010

Contributor

There's a typo. "associatin" should be "association".

This comment has been minimized.

Copy link
@fxn

fxn Aug 24, 2010

Member

Excellent! Would you like to correct it yourself in docrails?

This comment has been minimized.

Copy link
@vesan

vesan Aug 24, 2010

Contributor

I would but don't know how to do it properly. The file in http://github.com/lifo/docrails/blob/master/activerecord/lib/active_record/autosave_association.rb has different version of the documentation.

This comment has been minimized.

Copy link
@fxn

fxn Aug 24, 2010

Member

I see, there was a revision later and that misspelling is no longer there http://github.com/rails/rails/commit/8af2186d26c77f6fcb0787f50941ebe1a2905c5f

Thank you anyway.

This comment has been minimized.

Copy link
@vesan

vesan Aug 24, 2010

Contributor

I'm so sorry I wasted your time with this. I fixed another small typo in the same file though :-) http://github.com/vesan/docrails/commit/3de0c6d006a6e8c66031be0f95ad3f165c604cf2

This comment has been minimized.

Copy link
@fxn

fxn Aug 24, 2010

Member

No problem :), on the contrary really appreciate the comment.

Thanks for the other fix, all those pesky typos must die!

#
# post = Post.create(:title => 'ruby rocks')
# post.comments.create(:body => 'hello world')
# post.comments[0].body = 'hi everyone'
# post.save #=> will save both post and comment and comment will have 'hi everyone'
# #
# Destroying one of the associated models members, as part of the parent's # Destroying one of the associated models members, as part of the parent's
# save action, is as simple as marking it for destruction: # save action, is as simple as marking it for destruction:
Expand Down Expand Up @@ -125,6 +147,8 @@ module ActiveRecord
# post = Post.find(1) # post = Post.find(1)
# post.author.name = '' # post.author.name = ''
# post.save(:validate => false) # => true # post.save(:validate => false) # => true
#
# Note that validation will be perfomend even if <tt>autosave</tt> option is not declared.
module AutosaveAssociation module AutosaveAssociation
extend ActiveSupport::Concern extend ActiveSupport::Concern


Expand Down

1 comment on commit 06af291

@vesan
Copy link
Contributor

@vesan vesan commented on 06af291 Aug 24, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a typo. "associatin" should be "association".

Please sign in to comment.