module Sequel::Plugins::ManyThroughMany::DatasetMethods

Private Instance Methods

many_through_many_association_filter_expression(op, ref, obj) click to toggle source

Use a subquery to filter rows to those related to the given associated object

    # File lib/sequel/plugins/many_through_many.rb
424 def many_through_many_association_filter_expression(op, ref, obj)
425   lpks = ref[:left_primary_key_columns]
426   lpks = lpks.first if lpks.length == 1
427   lpks = ref.qualify(model.table_name, lpks)
428   edges = ref.edges
429   first, rest = edges.first, edges[1..-1]
430   ds = model.db[first[:table]].select(*Array(ref.qualify(first[:table], first[:right])))
431   rest.each{|e| ds = ds.join(e[:table], e.fetch(:only_conditions, (Array(e[:right]).zip(Array(e[:left])) + e[:conditions])), :table_alias=>ds.unused_table_alias(e[:table]), :qualify=>:deep, &e[:block])}
432   last_alias = if rest.empty?
433     first[:table]
434   else
435     last_join = ds.opts[:join].last
436     last_join.table_alias || last_join.table
437   end
438 
439   meths = if obj.is_a?(Sequel::Dataset)
440     ref.qualify(obj.model.table_name, ref.right_primary_keys)
441   else
442     ref.right_primary_key_methods
443   end
444 
445   expr = association_filter_key_expression(ref.qualify(last_alias, Array(ref.final_edge[:left])), meths, obj)
446   unless expr == SQL::Constants::FALSE
447     ds = ds.where(expr).exclude(SQL::BooleanExpression.from_value_pairs(ds.opts[:select].zip([]), :OR))
448     expr = SQL::BooleanExpression.from_value_pairs(lpks=>ds)
449     expr = add_association_filter_conditions(ref, obj, expr)
450   end
451 
452   association_filter_handle_inversion(op, expr, Array(lpks))
453 end
one_through_many_association_filter_expression(op, ref, obj)