callbacks

ruby
module EmailNormalization
  extend ActiveSupport::Concern

  included do
    attr_accessor :soft_warnings

Soft Validation: Normalize + Validate Email

rails activerecord validations
by codesnips 4 tabs
ruby
module WriteAmplificationGuard
  extend ActiveSupport::Concern

  def update_if_changed(attrs)
    changed = attrs.each_with_object({}) do |(key, value), acc|
      cast = self.class.type_for_attribute(key.to_s).cast(value)

“Write Amplification” Guard: Only Update Changed Columns

rails activerecord performance
by codesnips 3 tabs
ruby
class CreateTags < ActiveRecord::Migration[7.0]
  def change
    create_table :tags do |t|
      t.string :name, null: false
      t.integer :taggings_count, null: false, default: 0
      t.timestamps

Normalize Tags at Write Time

rails activerecord callbacks
by codesnips 3 tabs
ruby
class Document < ApplicationRecord
  belongs_to :account

  validates :source_url, presence: true

  before_save :normalize_checksum

Prevent Long Transactions with after_save_commit for Heavy Work

rails activerecord background-jobs
by codesnips 3 tabs
ruby
class CommentsChannel < ApplicationCable::Channel
  def subscribed
    post = find_post
    if post
      stream_for post
    else

Broadcasting New Comments to Subscribers over an ActionCable Channel

rails actioncable websockets
by codesnips 4 tabs
ruby
class RegistrationsController < ApplicationController
  def new
    @user = User.new
  end

  def create

Send a Welcome Email After Signup Using a Sidekiq Background Job in Rails

ruby rails sidekiq
by codesnips 4 tabs
ruby
class Order < ApplicationRecord
  has_many :line_items, dependent: :destroy, inverse_of: :order

  accepts_nested_attributes_for :line_items,
    reject_if: :all_blank,
    allow_destroy: true

Transactionally Create Parent + Children with accepts_nested_attributes_for

rails activerecord nested-attributes
by codesnips 3 tabs
ruby
class CreateAuditLogs < ActiveRecord::Migration[7.1]
  def change
    create_table :audit_logs do |t|
      t.references :auditable, polymorphic: true, null: false
      t.references :user, null: true, foreign_key: true
      t.string :action, null: false

Rails Audit Log for Model Changes Using ActiveRecord Callbacks

rails activerecord callbacks
by codesnips 4 tabs
ruby
class Order < ApplicationRecord
  has_many :line_items, inverse_of: :order, dependent: :destroy

  accepts_nested_attributes_for :line_items,
    allow_destroy: true,
    reject_if: ->(attrs) { attrs["product_id"].blank? && attrs["quantity"].blank? }

Validate Nested Attributes for an Order and Its Line Items in Rails

rails activerecord validations
by codesnips 3 tabs
ruby
module DomainEvents
  class Registry
    def initialize
      @subscribers = Hash.new { |h, k| h[k] = [] }
    end

Avoid Callback Chains: Use Domain Events (In-App)

rails architecture domain-events
by codesnips 4 tabs
ruby
class CreateAuditLogs < ActiveRecord::Migration[7.1]
  def change
    create_table :audit_logs do |t|
      t.references :auditable, polymorphic: true, null: false
      t.string :actor
      t.string :action, null: false

Audit Trail with JSON Diff (Minimal, Useful)

rails activerecord auditing
by codesnips 4 tabs
ruby
class AddSlugToPosts < ActiveRecord::Migration[7.1]
  def change
    add_column :posts, :slug, :string, null: false
    add_index :posts, :slug, unique: true
  end
end

Database-Backed Unique Slugs with Retry

rails postgres slugs
by codesnips 4 tabs