Module: ActiveRecordCompose::Persistence
- Extended by:
- ActiveSupport::Concern
- Includes:
- Callbacks
- Included in:
- Model
- Defined in:
- lib/active_record_compose/persistence.rb
Instance Method Summary collapse
-
#save(**options) ⇒ Boolean
Save the models that exist in models.
-
#save!(**options) ⇒ void
Behavior is same to #save, but raises an exception prematurely on failure.
-
#update(attributes) ⇒ Boolean
Assign attributes and #save.
-
#update!(attributes) ⇒ void
Behavior is same to #update, but raises an exception prematurely on failure.
Methods included from Callbacks
after_create, after_save, after_update, around_create, around_save, around_update, before_create, before_save, before_update
Instance Method Details
#save(**options) ⇒ Boolean
Save the models that exist in models. Returns false if any of the targets fail, true if all succeed.
The save is performed within a single transaction.
Only the :validate option takes effect as it is required internally.
However, we do not recommend explicitly specifying validate: false to skip validation.
Additionally, the :context option is not accepted.
The need for such a value indicates that operations from multiple contexts are being processed.
If the contexts differ, we recommend separating them into different model definitions.
30 31 32 33 34 |
# File 'lib/active_record_compose/persistence.rb', line 30 def save(**) with_callbacks { save_models(**, bang: false) } rescue ActiveRecord::RecordInvalid false end |
#save!(**options) ⇒ void
Behavior is same to #save, but raises an exception prematurely on failure.
41 42 43 |
# File 'lib/active_record_compose/persistence.rb', line 41 def save!(**) with_callbacks { save_models(**, bang: true) } || raise_on_save_error end |
#update(attributes) ⇒ Boolean
Assign attributes and #save.
51 52 53 54 |
# File 'lib/active_record_compose/persistence.rb', line 51 def update(attributes) assign_attributes(attributes) save end |
#update!(attributes) ⇒ void
Behavior is same to #update, but raises an exception prematurely on failure.
64 65 66 67 |
# File 'lib/active_record_compose/persistence.rb', line 64 def update!(attributes) assign_attributes(attributes) save! end |