Skip to content

Commit 4d5b441

Browse files
Clarify ::enum doc about validation behavior [ci-skip]
1 parent 312e115 commit 4d5b441

File tree

1 file changed

+13
-12
lines changed
  • activerecord/lib/active_record

1 file changed

+13
-12
lines changed

activerecord/lib/active_record/enum.rb

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,18 @@ module ActiveRecord
119119
# enum :status, [ :active, :archived ], instance_methods: false
120120
# end
121121
#
122-
# If you want the enum value to be validated before saving, use the option +:validate+:
122+
# By default, an +ArgumentError+ will be raised when assigning an invalid value:
123+
#
124+
# class Conversation < ActiveRecord::Base
125+
# enum :status, [ :active, :archived ]
126+
# end
127+
#
128+
# conversation = Conversation.new
129+
#
130+
# conversation.status = :unknown # 'unknown' is not a valid status (ArgumentError)
131+
#
132+
# If, instead, you want the enum value to be validated before saving, use the
133+
# +:validate+ option:
123134
#
124135
# class Conversation < ActiveRecord::Base
125136
# enum :status, [ :active, :archived ], validate: true
@@ -136,7 +147,7 @@ module ActiveRecord
136147
# conversation.status = :active
137148
# conversation.valid? # => true
138149
#
139-
# It is also possible to pass additional validation options:
150+
# You may also pass additional validation options:
140151
#
141152
# class Conversation < ActiveRecord::Base
142153
# enum :status, [ :active, :archived ], validate: { allow_nil: true }
@@ -152,16 +163,6 @@ module ActiveRecord
152163
#
153164
# conversation.status = :active
154165
# conversation.valid? # => true
155-
#
156-
# Otherwise +ArgumentError+ will raise:
157-
#
158-
# class Conversation < ActiveRecord::Base
159-
# enum :status, [ :active, :archived ]
160-
# end
161-
#
162-
# conversation = Conversation.new
163-
#
164-
# conversation.status = :unknown # 'unknown' is not a valid status (ArgumentError)
165166
module Enum
166167
def self.extended(base) # :nodoc:
167168
base.class_attribute(:defined_enums, instance_writer: false, default: {})

0 commit comments

Comments
 (0)