Skip to content

Instantly share code, notes, and snippets.

View mpressen's full-sized avatar

Maximilien Pressensé mpressen

View GitHub Profile
@mpressen
mpressen / model.rb
Last active March 18, 2022 10:59
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) }
@mpressen
mpressen / model.rb
Last active March 18, 2022 10:19
Polymorphic Joins
# frozen_string_literal: true
class Model < ApplicationRecord
extend PolymorphicJoins
belongs_to :profile, polymorphic: true # can be Client::Profile or Freelancer::Profile models
scope :clients, -> { polymorphic_joins(Client::Profile) }
end