Permalink
Browse files

Fix bug when using :select=>[] option for a many_to_many association

The reason this isn't hit by default is that the default :select
for many_to_many associations is associated_table.*.  The bug was
due to select_more being used instead of select_append.
  • Loading branch information...
jeremyevans committed Apr 14, 2011
1 parent c395e21 commit fa4fcc05eda1aa580c9a4035949222e2cf469759
Showing with 10 additions and 2 deletions.
  1. +2 −0 CHANGELOG
  2. +2 −2 lib/sequel/model/associations.rb
  3. +6 −0 spec/model/eager_loading_spec.rb
View
@@ -1,5 +1,7 @@
=== HEAD
+* Fix bug when using :select=>[] option for a many_to_many association (jeremyevans)
+
* Add a columns_introspection extension that attempts to skip database queries by introspecting selected columns (jeremyevans)
* When combining old integer migrations and new timestamp migrations, make sure old integer migrations are all applied first (jeremyevans)
@@ -690,9 +690,9 @@ def eager_loading_dataset(opts, ds, select, associations, eager_options={})
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)})
+ ds.select_append(*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_append(SQL::AliasedExpression.new(SQL::QualifiedIdentifier.new(opts.associated_key_table, opts.associated_key_column), opts.associated_key_alias))
end
end
ds
@@ -163,6 +163,12 @@ def fetch_rows(sql)
MODEL_DB.sqls.length.should == 2
end
+ it "should correctly handle a :select=>[] option to many_to_many" do
+ EagerAlbum.many_to_many :sgenres, :clone=>:genres, :select=>[]
+ a = EagerAlbum.eager(:sgenres).all
+ MODEL_DB.sqls.should == ['SELECT * FROM albums', "SELECT *, ag.album_id AS x_foreign_key_x FROM genres INNER JOIN ag ON ((ag.genre_id = genres.id) AND (ag.album_id IN (1)))"]
+ end
+
it "should eagerly load multiple associations in a single call" do
a = EagerAlbum.eager(:genres, :tracks, :band).all
a.should be_a_kind_of(Array)

0 comments on commit fa4fcc0

Please sign in to comment.