Jeremy, I tried your suggestions ( added graph_only_conditions, no dice .. here is result sequel for the Horse.eager_graph(:dude).first ( same result as with Horses.eager_graph(:dude).limit(1).all.first )
SELECT `HORSES`.`NAME`, `HORSES`.`KID - DATE`, `HORSES`.`KID - TIME`, `HORSES`.`KID - USER`, `HORSES`.`KID - MULT`, `HORSES`.`KID - COMM`, `HORSES`.`DUDE KID - DATE`, `HORSES`.`DUDE KID - TIME`, `HORSES`.`DUDE KID - USER`, `HORSES`.`DUDE KID - MULT`, `HORSES`.`DUDE KID - COMM`, `DUDE`.`KID - DATE` AS `DUDE_KID - DATE`, `DUDE`.`KID - TIME` AS `DUDE_KID - TIME`, `DUDE`.`KID - USER` AS `DUDE_KID - USER`, `DUDE`.`KID - MULT` AS `DUDE_KID - MULT`, `DUDE`.`KID - COMM` AS `DUDE_KID - COMM`, `DUDE`.`NAME` AS `DUDE_NAME`, `DUDE`.`RANCH CODE` FROM `HORSES` LEFT OUTER JOIN `DUDES` AS `DUDE` ON ((`KID - DATE`, `KID - TIME`, `KID - USER`, `KID - MULT`, `KID - COMM`) IS NULL) LIMIT 1 the join sql is messed up after the LEFT OUTER JOIN ... ON part. I did read that part on the advanced associations you were talking about, and it was confusing. I will try again though. Yes, you are right, for getting one record, its no big deal ( I am just trying to give super simple example so I can learn ) , but in reality I will be getting many records, and a few joins to various other tables at the same time, and it will make huge difference then. Daniel On Aug 31, 9:35 pm, Jeremy Evans <[email protected]> wrote: > On Aug 31, 8:34 pm, daniel_spaniel <[email protected]> wrote: > > > > > Hi > > I am trying to eager load an association which is linked by composite > > key. Lets say I have a models like these. > > > DB_DEMO.create_table! :dudes do > > Integer :'kid - date' > > Integer :'kid - time' > > Integer :'kid - user' > > String :'name' > > Integer :'ranch code' > > end > > > DB_DEMO.create_table! :horses do > > String :'name' > > Integer :'kid - date' > > Integer :'kid - time' > > Integer :'kid - user' > > Integer :'dude kid - date' > > Integer :'dude kid - time' > > Integer :'dude kid - user' > > end > > > class Dude < Sequel::Model(:dudes) > > set_primary_key [:'kid - date',:'kid - time',:'kid - user'] > > end > > > class Horse < Sequel::Model(:horses) > > set_primary_key [:'kid - date',:'kid - time',:'kid - user'] > > many_to_one( :dude, :key=>nil, :class => Dude, :dataset=> proc > > { Dude.filter( :"kid - date"=>self[:"dude kid - date"], :"kid - > > time"=>self[:"dude kid - time"], :"kid - user"=>self[:"dude kid - > > user"]) } ) > > end > > > I want to do something like > > Horses.first.dude > > and not do two queries to get the associated dude, but have the one > > query do the join when it loads the horse. > > You can probably do this with: > > Horses.eager_graph(:dude).limit(1).all.first > > You probably need the following association option for the :dude > association: > > :graph_only_conditions=>{:'kid - user'=>:'dude kid - user', > :"kid - date"=>:"dude kid - date", > :"kid - time"=>:"dude kid - time"} > > If you want to use .eager to eager load (separate queries instead of > joins), you'll need to write a custom :eager_loader, see the "Joining > on multiple keys" example in the Advanced Associations tutorial > (http://sequel.rubyforge.org/rdoc/files/doc/ > advanced_associations_rdoc.html). > > However, I'm not sure why you'd want to do this if you are only > loading a single horse and want the dude for just that horse. Are you > sure it performs better? > > Jeremy --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
