Active Record Callbacks

download Active Record Callbacks

of 15

Transcript of Active Record Callbacks

  • 7/31/2019 Active Record Callbacks

    1/15

    Active Record Callbacks

  • 7/31/2019 Active Record Callbacks

    2/15

    Use Cases of Callbacks / Observers

    Force a Rollback (prevent the model from beingsaved) under certain circumstances

    Track Activities, Log Changes, Send

    Notifications Ensure the existance of associated objects

    Inform associated models about changes

    Save Information about the circumstances themodel was saved / created (e.g. date / time)

    Trigger a state change

    Process raw data into more usabale data (e.g.

  • 7/31/2019 Active Record Callbacks

    3/15

    Overview

    Creating an Object

    before_validation

    After_validation

    before_save

    around_save

    before_create

    around_create

    after_create

    after_save

    Updating an Object before_validation after_validation before_save around_save before_update

    around_update after_update after_save

    Destroying an Object before_destroy around_destroy after_destroy

    Initializing an Object after_initialize after_find

  • 7/31/2019 Active Record Callbacks

    4/15

    Callback Chain

    Stolen from the rails guide:

  • 7/31/2019 Active Record Callbacks

    5/15

    Callback Chain

    Stolen from the rails guide:

    or: before_update

    or: after_update

  • 7/31/2019 Active Record Callbacks

    6/15

    Callback Chain

    Stolen from the rails guide:

    or: after_rollback

    The only twocallbacks called afterthe transaction

    or: before_update

    or: after_update

  • 7/31/2019 Active Record Callbacks

    7/15

    Callback Chain

    Stolen from the rails guide:

    or: after_rollback

    The only twocallbacks called afterthe transaction

    Additional to the callbacks in thesave / destroy workflow:

    - after_find- after_initialize

    => after a object was found, and initiatedby a finder, while after_initialize is triggeredfor new_records as well.

    or: before_update

    or: after_update

  • 7/31/2019 Active Record Callbacks

    8/15

    Transactions

    Are a database feature

    Wrapped by ActiveRecord::Transactions

    Typical Example for Transaction stolen from Railsguides:

    Read more about Transactions and the AR-Support for different Databases here:http://api.rubyonrails.org/classes/ActiveRecord/Trans

    http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.htmlhttp://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html
  • 7/31/2019 Active Record Callbacks

    9/15

    Rollback Transactions

    All Exceptions thrown within an transactions arecaught, and trigger a rollback, before they arere-raised by the transaction block (catch them)

    Exception: raising ActiveRecord::Rollbacktriggers a rollback, but is not re-raised

    Catching Exceptions inside the transaction,means it doesn't trigger a callback. Which mightcause unwanted behaviour / trouble (e.g.AR::StatementInvalid)

  • 7/31/2019 Active Record Callbacks

    10/15

    Callbacks and Transactions

    Still true: All after-/beforevalidate-/save-/update-/create callbacks are runinside a transaction

    Ways to cancel the transactions: Raise an exception ( be ready to catch it)

    Return false ( even if it is unwanted, be sure tocheck the return value of the last method call Inyour)

    Try to save or create an invalid record.

    Being in a transaction means all changes I

    apply to any other active record object, are

  • 7/31/2019 Active Record Callbacks

    11/15

    Custom Callbacks

    Note: If you include ActiveSupport::Callbacks this works with any kind of ruby class

  • 7/31/2019 Active Record Callbacks

    12/15

    Observer

    Allow to use the same callbacks as available inthe model itself

    Main purpose is to free models from callbacks

    that don't affect the model itself, e.g. sending anotification email.

    Are also wrapped inside the transaction.

  • 7/31/2019 Active Record Callbacks

    13/15

    Oberserver Examples

  • 7/31/2019 Active Record Callbacks

    14/15

    Callback Classes

    Similar to observers you can also pass a class(or instance of a class) to a callback

  • 7/31/2019 Active Record Callbacks

    15/15

    More

    If changing (associated) models in callbacks,never call .save or .create use build instead.Associated objects will be saved together withthe original model.

    Callbacks can take conditions (:if, and :unless),taking a method (symbol), proc or string (notrecommended)

    Never call .valid? Inside a after_validationcallback stack level too deep