Skip to content

Instantly share code, notes, and snippets.

@mpressen
Last active March 18, 2022 10:59
Show Gist options
  • Save mpressen/73e0b8dd40be7775a45f88ec72cf6674 to your computer and use it in GitHub Desktop.
Save mpressen/73e0b8dd40be7775a45f88ec72cf6674 to your computer and use it in GitHub Desktop.
Better Polymorphic Joins
# frozen_string_literal: true
class Model < ApplicationRecord
belongs_to :profile, polymorphic: true # can be Client::Profile or Freelancer::Profile models
# only to be used through joins
belongs_to :client_profile, -> { where("#{table_name}": { profile_type: 'Client::Profile' }) },
foreign_key: 'profile_id', class_name: 'Client::Profile', optional: true
scope :clients, -> { joins(:client_profile) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment