rails/activerecord/test/models/club.rb
Nikita Vasilevsky 2801ea64c3 Fix has_many through association assginments with custom scope and custom joining association name
`where_values_hash` method signature accepts `table_name` which is not
always the same as the association name.
So passing `through_association.reflection.name.to_s` as `table_name`
in `through_scope_attributes` wasn't accurate for every case.
This commit fixes the issue by passing the `table_name` taken from
`through_association.reflection.klass.table_name` instead.
2023-03-01 17:56:03 +00:00

32 lines
1.0 KiB
Ruby

# frozen_string_literal: true
class Club < ActiveRecord::Base
has_one :membership, touch: true
has_many :memberships, inverse_of: false
has_many :members, through: :memberships
has_one :sponsor
has_one :sponsored_member, through: :sponsor, source: :sponsorable, source_type: :Member
belongs_to :category
has_many :favorites, -> { where(memberships: { favorite: true }) }, through: :memberships, source: :member
has_many :custom_memberships, class_name: "Membership"
has_many :custom_favorites, -> { where(memberships: { favorite: true }) }, through: :custom_memberships, source: :member
scope :general, -> { left_joins(:category).where(categories: { name: "General" }).unscope(:limit) }
accepts_nested_attributes_for :membership
private
def private_method
"I'm sorry sir, this is a *private* club, not a *pirate* club"
end
end
class SuperClub < ActiveRecord::Base
self.table_name = "clubs"
has_many :memberships, class_name: "SuperMembership", foreign_key: "club_id"
has_many :members, through: :memberships
end