Last active
March 18, 2022 10:59
-
-
Save mpressen/73e0b8dd40be7775a45f88ec72cf6674 to your computer and use it in GitHub Desktop.
Better Polymorphic Joins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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