Permalink
Browse files

Cache negative lookup when eagerly loading many_to_one associations w…

…here no objects have an associated object
  • Loading branch information...
1 parent ca7cc40 commit 8cf0d3a91fc089ae83abd055b73a48c4b9330a9c @jeremyevans committed Sep 20, 2008
Showing with 6 additions and 4 deletions.
  1. +2 −0 CHANGELOG
  2. +4 −4 lib/sequel_model/associations.rb
View
2 CHANGELOG
@@ -1,5 +1,7 @@
=== HEAD
+* Cache negative lookup when eagerly loading many_to_one associations where no objects have an associated object (jeremyevans)
+
* Allow string keys to be used when using Dataset#multi_insert (jeremyevans)
* Fix join_table when doing the first join for a dataset where the first source is a dataset when using unqualified columns (jeremyevans)
View
8 lib/sequel_model/associations.rb
@@ -325,12 +325,12 @@ def def_many_to_one(opts)
opts[:eager_loader] ||= proc do |key_hash, records, associations|
h = key_hash[key]
keys = h.keys
+ # Default the cached association to nil, so any object that doesn't have it
+ # populated will have cached the negative lookup.
+ records.each{|object| object.associations[name] = nil}
# Skip eager loading if no objects have a foreign key for this association
unless keys.empty?
- # Default the cached association to nil, so any object that doesn't have it
- # populated will have cached the negative lookup.
- records.each{|object| object.associations[name] = nil}
- model.eager_loading_dataset(opts, opts.associated_class.filter(opts.associated_primary_key.qualify(opts.associated_class.table_name)=>keys), opts.select, associations).all do |assoc_record|
+ model.eager_loading_dataset(opts, opts.associated_class.filter(opts.associated_primary_key.qualify(opts.associated_class.table_name)=>keys), opts.select, associations).all do |assoc_record|
next unless objects = h[assoc_record.pk]
objects.each{|object| object.associations[name] = assoc_record}
end

0 comments on commit 8cf0d3a

Please sign in to comment.