Skip to content

Commit

Permalink
Update docs for nested_attributes to reflect default belongs_to behavior
Browse files Browse the repository at this point in the history
and validating parent model.

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
  • Loading branch information
machinehum and jonathanhefner committed Oct 1, 2021
1 parent 414e8de commit 7d9b5d4
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions activerecord/lib/active_record/nested_attributes.rb
Expand Up @@ -245,18 +245,19 @@ class TooManyRecords < ActiveRecordError
#
# === Validating the presence of a parent model
#
# If you want to validate that a child record is associated with a parent
# record, you can use the +validates_presence_of+ method and the +:inverse_of+
# key as this example illustrates:
#
# class Member < ActiveRecord::Base
# has_many :posts, inverse_of: :member
# accepts_nested_attributes_for :posts
# The +belongs_to+ association validates the presence of the parent model
# by default. You can disable this behavior by specifying <code>optional: true</code>.
# This can be used, for example, when conditionally validating the presence
# of the parent model:
#
# class Veterinarian < ActiveRecord::Base
# has_many :patients, inverse_of: :veterinarian
# accepts_nested_attributes_for :patients
# end
#
# class Post < ActiveRecord::Base
# belongs_to :member, inverse_of: :posts
# validates_presence_of :member
# class Patient < ActiveRecord::Base
# belongs_to :veterinarian, inverse_of: :patients, optional: true
# validates :veterinarian, presence: true, unless: -> { awaiting_intake }
# end
#
# Note that if you do not specify the +:inverse_of+ option, then
Expand Down

0 comments on commit 7d9b5d4

Please sign in to comment.