Skip to content

Commit 7d9b5d4

Browse files
Update docs for nested_attributes to reflect default belongs_to behavior
and validating parent model. Co-authored-by: Jonathan Hefner <[email protected]>
1 parent 414e8de commit 7d9b5d4

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

activerecord/lib/active_record/nested_attributes.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -245,18 +245,19 @@ class TooManyRecords < ActiveRecordError
245245
#
246246
# === Validating the presence of a parent model
247247
#
248-
# If you want to validate that a child record is associated with a parent
249-
# record, you can use the +validates_presence_of+ method and the +:inverse_of+
250-
# key as this example illustrates:
251-
#
252-
# class Member < ActiveRecord::Base
253-
# has_many :posts, inverse_of: :member
254-
# accepts_nested_attributes_for :posts
248+
# The +belongs_to+ association validates the presence of the parent model
249+
# by default. You can disable this behavior by specifying <code>optional: true</code>.
250+
# This can be used, for example, when conditionally validating the presence
251+
# of the parent model:
252+
#
253+
# class Veterinarian < ActiveRecord::Base
254+
# has_many :patients, inverse_of: :veterinarian
255+
# accepts_nested_attributes_for :patients
255256
# end
256257
#
257-
# class Post < ActiveRecord::Base
258-
# belongs_to :member, inverse_of: :posts
259-
# validates_presence_of :member
258+
# class Patient < ActiveRecord::Base
259+
# belongs_to :veterinarian, inverse_of: :patients, optional: true
260+
# validates :veterinarian, presence: true, unless: -> { awaiting_intake }
260261
# end
261262
#
262263
# Note that if you do not specify the +:inverse_of+ option, then

0 commit comments

Comments
 (0)