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

Avoid stack level too deep in predicate builder #41393

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion activerecord/lib/active_record/relation/predicate_builder.rb
Expand Up @@ -85,7 +85,9 @@ def expand_from_hash(attributes)

klass ||= AssociationQueryValue
queries = klass.new(associated_table, value).queries.map do |query|
expand_from_hash(query).reduce(&:and)
# If the query produced is identical to attributes don't go any deeper.
# Prevents stack level too deep errors when association and foreign_key are identical.
query == attributes ? build(table.arel_attribute(key), value) : expand_from_hash(query).reduce(&:and)
end
queries.reduce(&:or)
elsif table.aggregated_with?(key)
Expand Down
Expand Up @@ -2991,6 +2991,13 @@ def test_has_many_preloading_with_duplicate_records
assert_equal [1, 2], posts.first.comments.map(&:id)
end

def test_has_many_association_with_same_foreign_key_name
assert_nothing_raised do
firm = Firm.find(15)
assert_not_nil(firm.comments.first)
end
end

private
def force_signal37_to_load_all_clients_of_firm
companies(:first_firm).clients_of_firm.load_target
Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/fixtures/comments.yml
Expand Up @@ -63,3 +63,10 @@ sub_special_comment:
post_id: 4
body: Sub special comment
type: SubSpecialComment

recursive_association_comment:
id: 13
post_id: 5
body: afrase
type: Comment
company: 15
5 changes: 5 additions & 0 deletions activerecord/test/fixtures/companies.yml
Expand Up @@ -65,3 +65,8 @@ another_first_firm_client:
client_of: 1
name: Apex
firm_name: 37signals

recursive_association_fk:
id: 15
type: Firm
name: RVshare
1 change: 1 addition & 0 deletions activerecord/test/models/comment.rb
Expand Up @@ -14,6 +14,7 @@ class Comment < ActiveRecord::Base
belongs_to :post, counter_cache: true
belongs_to :author, polymorphic: true
belongs_to :resource, polymorphic: true
belongs_to :company, foreign_key: 'company'

has_many :ratings

Expand Down
1 change: 1 addition & 0 deletions activerecord/test/models/company.rb
Expand Up @@ -12,6 +12,7 @@ class Company < AbstractCompany
has_one :dummy_account, foreign_key: "firm_id", class_name: "Account"
has_many :contracts
has_many :developers, through: :contracts
has_many :comments, foreign_key: 'company'

attribute :metadata, :json

Expand Down
1 change: 1 addition & 0 deletions activerecord/test/schema/schema.rb
Expand Up @@ -213,6 +213,7 @@
t.datetime :updated_at
t.datetime :deleted_at
t.integer :comments
t.integer :company
end

create_table :companies, force: true do |t|
Expand Down