lib/sequel/plugins/many_through_many.rb in sequel-3.23.0 vs lib/sequel/plugins/many_through_many.rb in sequel-3.24.0

- old
+ new

@@ -222,11 +222,11 @@ module DatasetMethods private # Use a subquery to filter rows to those related to the given associated object - def many_through_many_association_filter_expression(ref, obj) + def many_through_many_association_filter_expression(op, ref, obj) lpks = ref[:left_primary_keys] lpks = lpks.first if lpks.length == 1 edges = ref.edges first, rest = edges.first, edges[1..-1] last = edges.last @@ -236,11 +236,16 @@ first[:table] else last_join = ds.opts[:join].last last_join.table_alias || last_join.table end - ds = ds.where(Array(ref[:final_edge][:left]).map{|x| ::Sequel::SQL::QualifiedIdentifier.new(last_alias, x)}.zip(ref.right_primary_keys.map{|k| obj.send(k)})) - SQL::BooleanExpression.from_value_pairs(lpks=>ds) + exp = association_filter_key_expression(Array(ref[:final_edge][:left]).map{|x| ::Sequel::SQL::QualifiedIdentifier.new(last_alias, x)}, ref.right_primary_keys, obj) + if exp == SQL::Constants::FALSE + association_filter_handle_inversion(op, exp, Array(lpks)) + else + ds = ds.where(exp).exclude(SQL::BooleanExpression.from_value_pairs(ds.opts[:select].zip([]), :OR)) + association_filter_handle_inversion(op, SQL::BooleanExpression.from_value_pairs(lpks=>ds), Array(lpks)) + end end end end end end