EugZol / where_exists

Adds the power of SQL Exists to ActiveRecord
MIT License
110 stars 18 forks source link

Fix the scope being yielded to the block #20

Closed TMaYaD closed 2 years ago

TMaYaD commented 3 years ago

Currently only the top level scope is being yielded to the block. This is un-intuitive in case of deeply nested associations and has_many...through relationships.

class Parent
  has_many :children
  has_many :grand_children, through: children
end

class Child
  belongs_to :parent
  has_many :grand_children
end

class GrandChild
  belongs_to :child
end

Parent.where_exists(:grand_children) do |assoc_scope|
  print assoc_scope.class # => Child::ActiveRecord_Relation # Should be GrandChild::ActiveRecord_Relation
end
EugZol commented 2 years ago

@TMaYaD Good catch, thank you very much. Would you please write a test case for that as well?

EugZol commented 2 years ago

@TMaYaD Merged (manually fetched commit from your branch). Thanks for the MR!