On Thursday, November 1, 2018 at 3:56:30 PM UTC-7, cvss wrote:
>
> I have a model called Test
>
> class Test < Sequel::Model
>
>
>  dataset_module do
>    def a
>      where{...}
>    end
>
>
>    def b
>      where{...}
>    end
>
>
>    def c 
>      where{...}
>    end
>  end
> end
>
> I want to create a fourth dataset combining a and b. Apparently
> def c 
>  (a & b) | c
> end
>
> in dataset_module returns a Sequel::SQL::BooleanExpression object.
> I want to be able to retrieve combined scope records without having to 
> rewrite the queries in each case.
> Am I able to do something like this in Sequel ?
>

You want the subset_conditions plugin:

class Test < Sequel::Model
  plugin :subset_conditions
  
  dataset_module do
    where :a, :a=>1
    where :b, :b=>2
    where :c, :c=>3

    def abnotc
      where((Sequel[a_conditions] & b_conditions) | c_conditions)
    end
  end
end

The reason you can't do `where((a & b) | c)` is because a, b, and c return 
datasets, not boolean expressions, and you use & (AND) and | (OR) to join 
boolean expressions together.

AND and OR aren't defined on queries in SQL.  If you have three queries, 
and you want the combine them in the way you describe, in SQL you need to 
use INTERSECT (~AND) and UNION (~OR), which Sequel supports:

  def abnotc
    a.intersect(b).union(c)
  end

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to