Inspecting model callback chains
If you need to look at the list of methods that are called upon certain events (like before/after saving etc), do this:
Model._save_callbacks.select {|cb| cb.kind == :before}.map{ |c| c.instance_variable_get :@filter }
Rails 2
User.after_save_callback_chain
To look at the method names only, you could do something like that:
User.after_save_callback_chain.collect(&:method)
Related cards:
Cancelling the ActiveRecord callback chain
Goal | Within before_*
|
Within after_*
|
---|---|---|
Cancel later callbacks | throw :abort |
throw :abort |
Rollback the tr... |
Save ActiveRecord models without callbacks or validations (in Rails 2 and Rails 3)
Rails 2
You can use
record.send(:update_without_callbacks)
or
record.send(:create_without_callbacks)
This can be used as a lightweight alternative to machinist's make
or FactoryGirl's create
, when you just need objects...
Order of the state_machine callback chain
- before transition
- before validation
- after validation
- before save
- after save
- after transition
Aborting the callback chain
See [Cancel the ActiveRecord callback chain](https://makandracards.com/makandr...
Testing ActiveRecord callbacks with RSpec
Our preferred way of testing ActiveRecord is to simply create/update/destroy the record and then check if the expected behavior has happened.
We used to bend over backwards to avoid touching the database for this. For this we used a lot of stubbi...
Apply a new callback to existing records
So you added a new callback to your model that (e.g.) caches some data when it is saved. Now you need to run that callback for the 10000 existing records in the production database. You have two options here:
- Write a clever migration, possibly...
Merging two arbitrary ActiveRecord scopes
(Rails has a method ActiveRecord::Relation#merge
that can merge ActiveRecord scopes. However, its behavior has never been clear, and in Rails 7 it still discards conditions on the same column by the last condition. **We disco...
7 Ways to Decompose Fat ActiveRecord Models - Code Climate Blog
“Fat models” cause maintenance issues in large apps. Only incrementally better than cluttering controllers with domain logic, they usually represent a failure to apply the Single Responsibility Principle (SRP). “Anything related to what a user do...
ActiveRecord::RecordNotFound errors allow you to query the :name and :id of the model that could not be found
ActiveRecord::RecordNotFound
errors provide quite meaningful error messages that can provide some insight on application details. Consider the following:
ActiveRecord::RecordNotFound: Couldn't find Organisation::Membership with 'id'=12 [WHE...
mceachen/closure_tree: Easily and efficiently make your ActiveRecord models support hierarchies
Closure_tree lets your ActiveRecord models act as nodes in a tree data structure.
This promises a few improvements over the battle-tested ancestry gem, such as...
Casting ActiveRecord scopes or instances to ActiveType extended model classes
When working with ActiveType you will often find it useful to cast an ActiveRecord instance to its extended ActiveType::Record
variant.
Starting with active_type 0.4.0 you can use ActiveType.cast
for...