Running with Ruby

Tag: ActiveRecord (page 1 of 9)

[ActiveRecord] Get a polymorphic owner class out of the owned object

The question is: how can we retrieve a owners class of a polymorphic association without loading the owner object into memory?

class Source < ApplicationRecord
  self.has_one :state,
    dependent: :delete,
    as: :stateable

class State < ApplicationRecord
  belongs_to :stateable,
    polymorphic: true


There are (at least) two ways to do this. First one is not recommended (as it might fail with multiple namespaces with same class names, etc:

State.last.stateable_type.constantize #=> Source

or a better one that will work out of any scope:

State.last.association(:stateable).klass #=> Source

Integrating Trailblazer and ActiveRecord transactions outside of a request lifecycle

When you use Ruby on Rails with ActiveRecord, you can get used to having separate transaction on each request. This is valid also when using Trailblazer (when inside of a request scope), however Trailblazer on its own does not provide such functionality. It means that when you’re using it from the console and/or when you process stuff inside background workers, you no longer have an active transaction wrapping around an operation.

This behavior is good most of the time. Since background tasks can run for a long period of time, there might be a risk of unintentional locking a big part of your database. However, sometimes you just need to have transactions.

In order to provide this feature for each operation, we will use a concern that will include that logic. We will also make it configurable, so if we inherit from a given operation, we will still have an option to disable/enable transaction support based on the operation requirements.

The code itself is pretty simple – it will just wraps around a #run method of the operation class with a transaction (as long as transaction support is enabled). Note, that by default transactional flag is set to false.

module Concerns
  module Transactional
    extend ActiveSupport::Concern

    included do
      class_attribute :transactional

      self.transactional = false

    def run
      if self.class.transactional
        self.class.transaction do

    class_methods do
      def transaction
        ActiveRecord::Base.transaction do
          return yield

In order to use it, just include it into your operation:

class ApplicationOperation < Trailblazer::Operation
  # Including on its own won't turn transactions on
  include Concerns::Transactions

class DataOperation < ApplicationOperation
  # This operation will have a single transaction wrapping it around
  self.transactional = true

Copyright © 2017 Running with Ruby

Theme by Anders NorenUp ↑