Skip to content

Conversation

@etoundi2nd
Copy link
Contributor

has_many with scoped declaring virtual attributes are rightfully not present in the columns_hash.
This causes the connected_through_array? to crash.

Setup to replicate the issue

# models/company.rb
has_many :companies_users
has_many :users, through: :companies_users

# models/companies_user.rb
belongs_to :user
belongs_to :company

# models/event.rb
has_many :companies, -> { polymorphic_company_scope }, primary_key: 'user_account_id', foreign_key: 'virtual_user_account_id' 

def self.polymorphic_company_scope
    companies_q = <<-SQL.squish
        SELECT
            companies.*,
            companies_users.user_id AS v_user_account_id
        FROM
            companies
            INNER JOIN companies_users ON companies.id = companies_users.company_id
    SQL

    -> { from("(#{ActiveRecord::Base.connection.quote_string(companies_q)}) companies") }
end

The virtual attribute virtual_user_account_id will cause the crash

@crashtech crashtech self-assigned this Aug 12, 2024
@crashtech crashtech merged commit cdb8a32 into crashtech:master Aug 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants