Skip to content

Module Poly::Joins

  • Extended by: ActiveSupport::Concern
  • Defined in: lib/poly/joins.rb

Type-safe INNER JOIN generation for polymorphic belongs_to associations.

Including this module defines a joins_<association> class method for every polymorphic belongs_to declared before the include.

class Status < ApplicationRecord
belongs_to :resource, polymorphic: true
include Poly::Joins
end
Status.joins_resource(Account)

The target model must declare the inverse association (has_many :statuses, as: :resource), otherwise Poly::PolymorphicJoinError is raised.

Public Class Methods

joins_resource

joins_resource(klass)

Generated for each polymorphic belongs_to; shown here for belongs_to :resource, polymorphic: true. Builds an INNER JOIN from this model to klass’s base class, matching both the _id and _type columns.

  • @param klass [ClassActiveRecord::Base] the owning model to join to
  • @raise [Poly::PolymorphicJoinError] if klass is not an ActiveRecord model or does not declare the inverse association
  • @return [ActiveRecord::Relation]