Got it. Thank you! > On Jul 6, 2022, at 2:49 PM, Jeremy Evans <jeremyeva...@gmail.com> wrote: > > >> On Wed, Jul 6, 2022 at 12:24 PM Matt Culpepper <m...@culpepper.co> wrote: > >> Thanks, that mostly works, except it drops the `WHERE (("table2"."user_id" = >> <id>)` >> >> I can add a .where(user_id: ...) but I'm not sure how to specify that I want >> the actual id of the user in this context >> >> Sorry if the answer is obvious! > > It's not obvious, so don't feel bad. You probably want to add > .where(Sequel[:table2][:user_id] => id). That should work for lazy loading. > For eager loading via #eager, you'll need to use the :eager_loader option, > and for #eager_graph, you'll probably want to use the :graph_only_conditions > option. > > Thanks, > Jeremy > >> >>> On Wednesday, July 6, 2022 at 12:56:13 PM UTC-5 Jeremy Evans wrote: >>>> On Wed, Jul 6, 2022 at 10:38 AM Matt Culpepper <ma...@culpepper.co> wrote: >>>> Hey Jeremy, I'm trying to set up a relationship in a model that generates >>>> SQL like the following join with an OR between the conditions. >>>> >>>> SELECT * FROM table1 INNER JOIN table2 ON (("table2"."resource_id" = >>>> "table1"."an_id") OR ("table2"."resource_id" = "table1"."another_id")) >>>> WHERE (("table2"."user_id" = <id>) >>>> >>>> This is the closest I've come: >>>> >>>> User.many_to_many(:table1, join_table: :table2, right_key: [:resource_id, >>>> :resource_id], right_primary_key: [:an_id, :another_id]) >>>> >>>> However, this generates an AND >>>> >>>> SELECT * FROM table1 INNER JOIN table2 ON (("table2"."resource_id" = >>>> "table1"."an_id") AND ("table2"."resource_id" = "table1"."another_id")) >>>> WHERE (("table2"."user_id" = <id>) >>>> >>>> Any ideas on how I can generate the query with the OR? >>> >>> You can use the :dataset option to specify an arbitrary dataset to use. >>> Something like this may work: >>> >>> User.many_to_many(:table1, >>> join_table: :table2, >>> right_key: [:resource_id, :resource_id], >>> right_primary_key: [:an_id, :another_id], >>> dataset: lambda{|r| r.associated_class.join(:table2, >>> Sequel.expr{(table2[:resource_id] =~ table1[:an_id]) | >>> (table2[:resource_id] =~ table1[:another_id])})} >>> ) >>> >>> You could also use the following for IN instead of OR: >>> >>> lambda{|r| r.associated_class.join(:table2, {:resource_id=>[:an_id, >>> :another_id]}, :qualify=>:deep)} >>> >>> 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 sequel-talk+unsubscr...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/sequel-talk/73cade86-c91e-4319-95d9-50200424d694n%40googlegroups.com. > > -- > You received this message because you are subscribed to a topic in the Google > Groups "sequel-talk" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/sequel-talk/dVjFBpkfgLs/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > sequel-talk+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sequel-talk/CADGZSScBFdGsoSZMHpdKXgizrc4qu2kRsgf9Bf%2BL3yaXw8Ldng%40mail.gmail.com.
-- 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 sequel-talk+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sequel-talk/C456D6B2-3889-43FB-9306-C16BD3C5C7ED%40culpepper.co.