Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure queries virtual attributes do not cause a crash #93

Merged
merged 1 commit into from
Aug 12, 2024

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
8 checks passed
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