Permalink
Browse files

Add associated columns later to avoid eager loading callback needing …

…to reselect them.
  • Loading branch information...
1 parent 5e626da commit c395e21c2ce10da599415905136b2e30095f7e33 John Firebaugh committed with Apr 14, 2011
Showing with 7 additions and 6 deletions.
  1. +6 −5 lib/sequel/model/associations.rb
  2. +1 −1 spec/model/eager_loading_spec.rb
@@ -683,17 +683,18 @@ def eager_loading_dataset(opts, ds, select, associations, eager_options={})
if opts[:eager_graph]
ds = ds.eager_graph(opts[:eager_graph])
ds = ds.add_graph_aliases(opts.associated_key_alias=>[opts.associated_class.table_name, opts.associated_key_alias, SQL::QualifiedIdentifier.new(opts.associated_key_table, opts.associated_key_column)]) if opts.eager_loading_use_associated_key?
- elsif opts.eager_loading_use_associated_key?
+ end
+ ds = ds.eager(associations) unless Array(associations).empty?
+ ds = opts[:eager_block].call(ds) if opts[:eager_block]
+ ds = eager_options[:eager_block].call(ds) if eager_options[:eager_block]
+ if !opts[:eager_graph] && opts.eager_loading_use_associated_key?
ds = if opts[:uses_left_composite_keys]
t = opts.associated_key_table
ds.select_more(*opts.associated_key_alias.zip(opts.associated_key_column).map{|a, c| SQL::AliasedExpression.new(SQL::QualifiedIdentifier.new(t, c), a)})
else
- ds.select_more(SQL::AliasedExpression.new(SQL::QualifiedIdentifier.new(opts.associated_key_table, opts.associated_key_column), opts.associated_key_alias))
+ ds.select_more(SQL::AliasedExpression.new(SQL::QualifiedIdentifier.new(opts.associated_key_table, opts.associated_key_column), opts.associated_key_alias))
end
end
- ds = ds.eager(associations) unless Array(associations).empty?
- ds = opts[:eager_block].call(ds) if opts[:eager_block]
- ds = eager_options[:eager_block].call(ds) if eager_options[:eager_block]
ds
end
@@ -685,7 +685,7 @@ def fetch_rows(sql)
end
it "should eagerly load a many_to_many association with custom eager block" do
- a = EagerAlbum.eager(:genres => proc {|ds| ds.select(:name, ds.opts[:select].last)}).all
+ a = EagerAlbum.eager(:genres => proc {|ds| ds.select(:name)}).all
a.should be_a_kind_of(Array)
a.size.should == 1
a.first.should be_a_kind_of(EagerAlbum)

0 comments on commit c395e21

Please sign in to comment.