Skip to content

duplicate JOIN statement searching on multiple has_one though association #986

@Liooo

Description

@Liooo

Is this expected?

class Children < ActiveRecord::Base
  belongs_to :parent
  has_one :a_ancestor, through: :parent
  has_one :b_ancestor, through: :parent
  has_one :c_ancestor, through: :parent
end

class Parent < ActiveRecord::Base
  belongs_to :a_ancestor
  belongs_to :b_ancestor
  belongs_to :c_ancestor
end

class AAncestor < ActiveRecord::Base; end
class BAncestor < ActiveRecord::Base; end
class CAncestor < ActiveRecord::Base; end

Children.ransack(
  a_ancestor_id_eq: 1,
  b_ancestor_id_eq: 1,
  c_ancestor_id_eq: 1,
).result.to_sql

# => LEFT OUTER JOINs `parents` 3 times

Here's executable test of the issue

https://gist.github.com/Liooo/ea4629204f7f3233633125e5f134b574

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions