SlideShare a Scribd company logo
ActiveRecord::Callbacks
Готовим правильно
Павел Габриель / @alovak
AR::Callbacks
before_create / save / update / validate
after_*
ActiveRecord::Callbacks. Готовим правильно.
Что не так с AR::Callbacks?
“That’s a total of twelve callbacks,
which gives you immense power
to react and prepare for each state
in the ActiveRecord life cycle.”
— active_record/callbacks.rb
ActiveRecord::Callbacks. Готовим правильно.
ActiveRecord::Callbacks. Готовим правильно.
ActiveRecord::Callbacks. Готовим правильно.
ActiveRecord::Callbacks. Готовим правильно.
ActiveRecord::Callbacks. Готовим правильно.
“That’s Ruby, which gives you
immense power to do, you know,
pretty much anything, ever”
— Ernie Miller
Google
“Are ActiveRecord Callbacks Any Good?”
“ActiveRecord Callbacks are Unreasonable”
“Rails, callbacks, workers, and the race you
never expected to lose”
“ActiveRecord's Callbacks
Ruined My Life”
ActiveRecord::Callbacks. Готовим правильно.
ActiveRecord::Callbacks. Готовим правильно.
ActiveRecord::Callbacks. Готовим правильно.
ActiveRecord::Callbacks. Готовим правильно.
ActiveRecord::Callbacks. Готовим правильно.
В чем же проблема?
Проблема? 
Single Responsibility Principle
“A class should do the smallest
possible useful thing; that is,
it should have
a single responsibility.”
— Sandi Metz
SRP Violation

К лишнему весу ..
SRP Violation

К усложнению кода
SRP Violation
К сложным тестам
Невозможности повторного
использования
И т.д.
Варианты решений
Вариант с Dependency
Injection
ActiveRecord::Callbacks. Готовим правильно.
ActiveRecord::Callbacks. Готовим правильно.
ActiveRecord::Callbacks. Готовим правильно.
ActiveRecord::Callbacks. Готовим правильно.
ActiveRecord::Callbacks. Готовим правильно.
Вариант с Service
ActiveRecord::Callbacks. Готовим правильно.
ActiveRecord::Callbacks. Готовим правильно.
ActiveRecord::Callbacks. Готовим правильно.
Вариант с Events/Signals
ActiveRecord::Callbacks. Готовим правильно.
ActiveRecord::Callbacks. Готовим правильно.
ActiveRecord::Callbacks. Готовим правильно.
ActiveRecord::Callbacks. Готовим правильно.
ActiveSupport::Notifications
ActiveRecord::Callbacks. Готовим правильно.
ActiveRecord::Callbacks. Готовим правильно.
Wisper (gem)
ActiveRecord::Callbacks. Готовим правильно.
ActiveRecord::Callbacks. Готовим правильно.
• Минимизируйте зависимости с
помощью Dependency Injection
• Объединяйте несколько моделей в
Service
• Используйте Events/Signals
“Rails makes it natural and easy to build
large, well-designed OOP systems.”
— ????????
“Rails makes it natural and easy to build
large, well-designed OOP systems.”
— Nobody
Rails has no brain;
use your own!
Спасибо!
alovak@gmail.com

More Related Content

PDF
Rubyslava debugging with_pry
PDF
Surviving SOA - delivering (somewhat) continuously on a hostile planet
PDF
From ActiveRecord to EventSourcing
PDF
Immutable servers with Packer/Chef/AWS
PPTX
Social factors in groups
PPTX
Pomodoro technique
PDF
10 reasons I love RubyOnRails
PDF
Successful development without QA
Rubyslava debugging with_pry
Surviving SOA - delivering (somewhat) continuously on a hostile planet
From ActiveRecord to EventSourcing
Immutable servers with Packer/Chef/AWS
Social factors in groups
Pomodoro technique
10 reasons I love RubyOnRails
Successful development without QA

Recently uploaded (20)

PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Modernizing your data center with Dell and AMD
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Empathic Computing: Creating Shared Understanding
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Electronic commerce courselecture one. Pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Modernizing your data center with Dell and AMD
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Unlocking AI with Model Context Protocol (MCP)
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Empathic Computing: Creating Shared Understanding
20250228 LYD VKU AI Blended-Learning.pptx
MYSQL Presentation for SQL database connectivity
Mobile App Security Testing_ A Comprehensive Guide.pdf
Approach and Philosophy of On baking technology
Encapsulation_ Review paper, used for researhc scholars
Diabetes mellitus diagnosis method based random forest with bat algorithm
Building Integrated photovoltaic BIPV_UPV.pdf
NewMind AI Monthly Chronicles - July 2025
Electronic commerce courselecture one. Pdf
Network Security Unit 5.pdf for BCA BBA.
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Ad
Ad

ActiveRecord::Callbacks. Готовим правильно.