-
Notifications
You must be signed in to change notification settings - Fork 1
Method Return Values
CodeMeister edited this page Feb 28, 2023
·
1 revision
Predicate methods, those that end with a question mark, will always return
true
orfalse
. They will never return any other value or raise any exceptions.
post.draft? #=> true
post.archived? #=> false
Getter methods, those that expect a value to be returned, will always return a 'truthy' value such as a
String
,Symbol
,Array
orHash
, as specified in the documentation above. They will never returnnil
,false
nor raise any exceptions.
Post.status_transitions #=> { draft: [:archived, :pending],
#=> pending: [:draft, :published],
#=> published: [:pending, :archived],
#=> archived: [:published, :draft] }
Setter methods, those that make changes, will always return the expected
ActiveRecord
response if successfull. If the new state is invalid or the transition is not allowed, anArgumentError
exception will be raised.
Note: the exception is raised even if the change is not being persisted to the database.
post.state = :dummy #=> <ArgumentError>
post.update(state: :dummy) #=> <ArgumentError>
post.update_column :state, :dummy #=> <ArgumentError>