On Friday, August 30, 2013 8:30:32 AM UTC-7, Christian MICHON wrote:
>
> Hi,
>
> follow up on my previous post: I've now a piece of working code giving me 
> the subproducts for each instance of 'Product' (using a single Sequel model 
> 'Product', 2 MSSQL views and 1 many_to_many association).
>
> class Product < Sequel::Model
>   set_dataset DB[:tbl_products].select(:product_id, :name)
>   set_primary_key :product_id
>   many_to_many :subproducts, :class=>self, :select => 
> [:tbl_products__product_id, :name], :join_table=>:tbl_sub_products, 
> :left_key=>:product_id, :right_key=>:reference_product
> end
>
> I've been experimenting with eager and rcte_tree plugin. All I want is to 
> be able given a product ID to get an array of products, including this top 
> product and its 1st-level children. Next plans are to extract the whole 
> hierarchy (descendance) for scoping purposes.
>
> Rcte_tree is out of scope now as it request to define parent as well and 
> request dedicated parent_id on the table (and this is a view, out of my 
> control as I'm not an admin and I am only reading out data).
>
> With eager, all I tried gave no valid output... I'm sure some eager 
> loading is happening as each of the 3 following attempts get longer 
> response time:
>
> >> Product.where(:product_id=>82188).eager(:subproducts).all
> => [#<Product @values={:product_id=>82188, :name=>"********"}>]
>
> >> Product.where(:product_id=>82188).eager(:subproducts=>:subproducts).all
> => [#<Product @values={:product_id=>82188, :name=>"********"}>]
>
> >> 
> Product.where(:product_id=>82188).eager(:subproducts=>{:subproducts=>:subproducts}).all
> => [#<Product @values={:product_id=>82188, :name=>"********"}>]
>

This is completely expected.  All eager does is automatically populate the 
cached associations, it doesn't change how many records are returned by 
#all.  Compare the following:

Product.where(:product_id=>82188).all.map{|p| p.associations}
Product.where(:product_id=>82188).eager(:subproducts).all.map{|p| 
p.associations}

Product.where(:product_id=>82188).eager(:subproducts).all.map{|p| 
p.associations[:subproducts].map{|p| p.associations}}
Product.where(:product_id=>82188).eager(:subproducts=>:subproducts).all.map{|p| 
p.associations[:subproducts].map{|p| p.associations}}

That being said, if you are searching for a given product_id, all is only 
going to return a single record, so there is no point of eager loading if 
you are just loading a single level.  And if you are trying to eager load 
multiple levels, you may just want to use the tactical_eager_loading 
plugin, which will only use 4 queries for the following:

  Product[:product_id=>82188].subproducts.each do |sp|
    sp.subproducts.each do |ssp|
      ssp.subproducts.each do |sssp|
      end
    end
  end

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/groups/opt_out.

Reply via email to