Adding and Merging ActiveRecord Relations

The expressive possibilities of assembling ActiveRecord queries out of disparate parts make using Rails so fun.

Jared White by Jared White on October 24, 2020

An intriguing scenario arose in a project I was working on. In this application, creators can create plugin presets. Presets themselves are associated with “banks” — aka a bank has_many presets. There are also actions that can be taken against either banks or presets: users generally will bookmark them or download them. Thus these actions have a polymorphic association with both presets and banks.

What I wanted to do was package up all the actions users had performed for banks and related presets created by a particular creator. The resulting ActiveRecord query I arrived at expresses that well. Could this be refactored in a more performant way? (For example, using only one SQL query instead of three?) Very likely. But as this code is run only infrequently for reporting reasons, it’s a reasonable tradeoff between performance and readability.

Here’s the code for the ItemAction.for_creator(creator_profile) method:

class ItemAction < ApplicationRecord
  belongs_to :actionable, polymorphic: true
  enum action_type: [ :bookmark, :download, :publish ]

  def self.for_creator(creator_profile)
    banks =
    presets = Preset.where(bank: banks).select(:id)

    where(actionable: banks + presets).merge(

  # other code, etc.

First, we get the list of banks. We use a published scope to limit the list to only banks which have been made available to the public. Also, we only need the id field back, not all the fields in the database table.

Second, we get the list of presets for those banks, again pulling in only the id field.

The last line of the method is where things get interesting. We want actions where the actionable association brings in both the banks and the presets we’ve loaded, so we use the + operator to concatenate both result sets together. Once we have that relation, we can use ActiveRecord’s merge functionality to pull in additional scopes that specify we only want either bookmark or download actions. We don’t want any other actions (say, publish), because those are actions taken by creators, not end users.

And that does the trick! Yay! A brief but useful example of the expressive power of adding and merging ActiveRecord relations together to get a final result.

“Ruby is simple in appearance, but is very complex inside, just like our human body.”


Join nearly 200 fullstack Ruby developers and subscribe to receive a timely tip you can apply directly to your Ruby site or application each week:

Banner image by Michael Dziedzic on Unsplash

Other Recent Articles

How Ruby and Web Components Can Work Together

Use Ruby to transform incoming data and generate reusable markup, and use web components to encapsulate frontend styling and behavior. Build discrete building blocks of UI while collapsing mental models. Chef’s kiss!

Continue Reading

Episode 2: Componentized View Architecture FTW!

There are no full stack engineers?! Let’s talk about that. Also, just what is a componentized view architecture anyway? I break it all down and explain why I’m gung-ho about view components. Plus I answer questions regarding Stimulus, nice_partials, and other Rails tooling from listeners like YOU! Enjoy, and keep on Ruby-ing!

Continue Reading

More This Way