On Jul 31, 2012, at 9:05 , Jeremy Evans wrote: > If you know the SQL you want, then just using it is not a problem. You could > also express such a query using Sequel's DSL: > > DB[:orders]. > join(:items, :order_id=>:id). > join(:skus, :id=>:sku_id). > where(:orders__id=>2543). > sum(:skus__weight) > > The difference between this query and the above using dataset_associations is > that this one does a single query with two joins, while the dataset > associations one uses 2 queries (first one to get the order), with the second > query using a subselect.
Exactly. That's what I don't want to do. I'd spent a lot of extra time figuring out how to translate the SQL into Sequel, and end up with a less efficient query. > Note that you don't appear to need to join to orders, Oh, true. Duh. >> But I really like models. I just haven't figured out how to reach through a >> model's associations to the associations beyond that except by bypassing the >> whole thing and using SQL. > > Models are just designed to be a thin wrapper around a dataset and individual > dataset rows that adds some nice behavior. Very nice behavior. I think being able to say order.buyer = buyer is much more elegant than order.buyer_id = buyer.id, and managing many-to-many joins is also notably more elegant. > For what you are doing (aggregations), they aren't that helpful beyond a > place to store methods. For example, I would probably implement what you > want as an instance method on Order: > > class Order > def weight > DB[:items].join(:skus, :id=>:sku_id, id=>:order_id).sum(:skus__weight) > end > end Oh! That's perfect. That join clause above looks like voodoo to me; I just haven't spent that much time manipulating datasets that directly. I'll dig into the docs to make sure I understand it, but hiding it all as an instance method is just what I want. Thank you! -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sequel-talk?hl=en.
