In Rails, in what order do inherited before_action callbacks run, and how do prepend_before_action and skip_before_action change that chain?
answer
- parent's callbacks first, then the child's
- prepend_before_action jumps to the front
- after_action runs in reverse order
- redeclaring a name moves it
- skip raises unless raise: false
basics
~20 sA subclass inherits its parent's callback chain and appends its own, so ApplicationController's callbacks run first; prepend_before_action inserts at the front, and skip_before_action removes a named callback, or narrows it with only:, raising ArgumentError if it is not in the chain.
solid answer
~40 sCallbacks are stored per class and copied to subclasses, so a controller starts with `ApplicationController`'s chain and `before_action` **appends** to it: parent callbacks first, in declaration order, then the child's. Before and around callbacks run in that order; **after callbacks run in reverse**. `prepend_before_action :set_shop` inserts at the very front — ahead of the parent's callbacks — which is how a child loads `@shop` before an inherited guard reads it. `skip_before_action :require_staff, only: %i[index show]` removes the callback for those actions by adding a condition; without `only:`/`except:` it removes it entirely. If no callback with that name exists, it raises `ArgumentError` — `raise: false` makes it a no-op. Redeclaring an existing symbol replaces it **and moves it to the end** with the new options.
code
ruby · 10 linesclass ApplicationController < ActionController::Base
before_action :require_staff_for_writes
end
class BouquetsController < ApplicationController
prepend_before_action :set_shop # runs first, before the inherited guard
skip_before_action :require_staff_for_writes, only: %i[ index show ]
skip_before_action :track_campaign, raise: false # may not exist in this chain
before_action :set_bouquet, only: %i[ show edit update destroy ]
endgo deeper
Know that ApplicationController's callbacks run first and that skip_before_action lets a controller opt out of an inherited one.
Explain append versus prepend, reverse order for after callbacks, partial skips with only:, the ArgumentError on missing callbacks and raise: false.
Read and print the real chain when debugging, avoid redeclaration that silently reorders callbacks, and review every skip of an inherited guard as an access-control change.
Shape controller hierarchies so shared callbacks live at the right level, keeping skips rare enough that the effective chain of any controller stays obvious.
## Where the chain lives Every Action Controller class keeps its callbacks in a chain named `process_action`, stored in a class attribute. When `BouquetsController < ApplicationController` is defined, it receives a copy of the parent's chain; its own declarations then modify that copy and never the parent's. The chain can be inspected in a console: ```ruby BouquetsController._process_action_callbacks .select { |c| c.kind == :before } .map(&:filter) # => [:set_shop, #<Proc:... (lambda)>, :require_staff_for_writes, :set_bouquet] # the Proc is the before_action that allow_browser registers in the generated ApplicationController ``` ## The default order 1. Callbacks declared in `ApplicationController`, in declaration order. 2. Callbacks declared in any intermediate base controller, such as `Shops::BaseController`. 3. Callbacks declared in the controller itself, in declaration order. Before and around callbacks run in that order. **After callbacks run in reverse order**, so the last `after_action` declared is the first to run — the same nesting an around callback would give. ## Changing the order | Macro | Effect on the chain | |---|---| | `before_action :x` | appends `:x` at the end (alias `append_before_action`) | | `prepend_before_action :x` | inserts `:x` at the front, before inherited callbacks | | `skip_before_action :x` | removes `:x` | | `skip_before_action :x, only: %i[index]` | keeps `:x` but stops it for `index` | | `before_action :x, except: :index` again | removes the old `:x` and appends a new one with the new options | The same family exists for `after_action` and `around_action`: `prepend_after_action`, `skip_around_action` and so on. ## prepend_before_action in a florist storefront `ApplicationController` declares `before_action :require_staff_for_writes`, and that guard reads `@shop` to compare the staff member's shop. In `BouquetsController`, a plain `before_action :set_shop` would run **after** the inherited guard, which would see `nil`. `prepend_before_action :set_shop` puts the loader at the front of the chain, ahead of the inherited guard. The alternative — declaring the loader in the parent — is often cleaner when most controllers need it. ## skip_before_action and its sharp edges - **By name only.** Skipping matches the callback's filter, so a symbol callback can be skipped by its symbol. A callback registered as a block or lambda has no name to pass. - **Partial skips.** With `only:` or `except:`, Rails keeps the callback and adds the opposite condition, so `skip_before_action :require_staff, only: %i[index show]` lets customers browse while edits stay guarded. - **Missing callbacks raise.** `skip_before_action :require_staff` in a controller whose chain has no `:require_staff` raises `ArgumentError` ("Before process_action callback :require_staff has not been defined"). Pass `raise: false` when the callback may legitimately be absent, for example in a concern shared by controllers with different parents. ## Redeclaring a callback moves it Declaring `before_action :set_shop, except: :index` in a subclass, when the parent already declared `:set_shop`, does not add a second copy. Rails removes the existing callback with that name and **appends** the new one, so `set_shop` now runs after every callback the subclass declared before it. Teams that use redeclaration to change options are often surprised by the new position; `skip_before_action` with `only:` changes the scope without moving anything. The same rule explains a quieter variant: a concern included into the controller that declares a callback with the same name as an inherited one replaces it rather than adding to it, and the replacement's position and options win. Printing the chain is the fastest way to see which declaration survived. ## Debugging an ordering bug A typical report: "the staff guard says every request is unauthorised in `BouquetsController` only". The steps: 1. Print the chain with `_process_action_callbacks` and note where the guard and the loader sit. 2. If the guard reads `@shop` and comes first, it sees `nil`; move the loader with `prepend_before_action` or into the parent. 3. Check for a redeclared callback that moved, or a `skip_before_action` with `only:` that no longer matches renamed actions. 4. Add a request test per role so the fixed order is pinned down. ## Common mistakes - Assuming a subclass's `before_action` runs before the parent's. - Expecting after callbacks to run in declaration order. - Using `skip_before_action` on an inherited guard to "fix" a test and leaving whole actions unguarded. - Redeclaring a callback to tweak `only:` and silently reordering the chain.
- In Rails, in what order do two after_action callbacks declared as :log_a then :log_b run?In reverse: `log_b` first, then `log_a`. Active Support runs before and around callbacks in the order they were set and after callbacks in reverse, so the outermost declaration finishes last, mirroring how nested around callbacks unwind.
- How can you see a Rails controller's full before_action chain, including inherited callbacks?In a console, `SomeController._process_action_callbacks` returns the chain; select callbacks whose `kind` is `:before` and map them to `filter` to get the names in run order. Lambda and block callbacks show up as procs rather than symbols.
- A subclass's before_action runs before the callbacks inherited from ApplicationController.
- after_action callbacks run in the order they are declared.
- skip_before_action on a callback that does not exist is silently ignored.
- Declaring the same before_action again adds a second copy that runs twice.
- skip_before_action in a subclass also removes the callback from the parent controller.