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
-~----------~----~----~----~------~----~------~--~---