Read more

Pitfall: ResourceController overwrites where ActiveRecord enqueues

Deleted user #6
January 31, 2013Software engineer

Defining one callback several times in the same class behaves different in ActiveRecord and ResourceController.
While in ActiveRecord the callbacks are enqueued, they overwrite each other in ResourceController.

ActiveRecord - a common practice

class Post < ActiveRecord::Base
  does 'post/behavior'
  before_validation :do_something
end

module Post::BehaviorTrait
  as_trait do
    before_validation :do_something_else
  end
end
Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

do_something_else and do_something are executed before validation in exactly this order

ResourceController - here you have to be careful

class PostController
  include ResourceController::Controller
  does 'post_controller/behavior'
  index.before { do_something }
end

module PostController::BehaviorTrait
  as_trait do
    index.before { do_something_else }
  end
end

Only do_something is executed before index because the callback defined in the trait is overwritten.

Posted to makandra dev (2013-01-31 11:42)