On Thursday, January 22, 2015 at 8:11:27 PM UTC-8, Craig Povey wrote:
>
> Hi Jeremy, 
>
> I had a quick question about using associations - I'm trying to add an 
> association that joins on two keys, but where one of those two keys should 
> also allow NULL values. So essentially the dataset I want it to return 
> should be:
>
> SELECT * FROM base_table INNER JOIN foos ON (base_table.key_1 = foos.key_1 
> AND (base_table.key_2 = foos.key_2 OR foos.key_2 IS NULL))
>
>
> Or in other words, the query to return the dataset of foos associated with 
> a given record in the base table (e.g. the record where key_1 is 1 and 
> key_2 is 1) would be:
>
> SELECT * FROM foos WHERE (foos.key_1 = 1 AND (foos.key_2 = 1 OR foos.key_2 
> IS NULL)) 
>
>
> I currently have the association specified as follows:
>
> one_to_many :foos,
>                        :key               => [:key_1, :key_2],
>                        :primary_key => [:key_1, :key_2]
>
>
> but as you can imagine, this returns no association results in the case 
> where key_2 is NULL. What's the right way to specify this?
>

You probably need to use a custom :dataset option:

   :dataset=>proc{Foo.where(:key1=>key1).where(Sequel.or(key2=>:key2, 
:key2=>nil))}

If you want to eager load such an association, you'll need to provide your 
own :eager_loader and/or :eager_grapher.

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 http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to