Ticket #9923: lazyattr.diff
| File lazyattr.diff, 5.6 kB (added by RubyRedRick, 1 year ago) |
|---|
-
test/associations/lazy_association_test.rb
old new 1 require 'abstract_unit' 2 require 'fixtures/post' 3 require 'fixtures/comment' 4 require 'fixtures/author' 5 require 'fixtures/category' 6 require 'fixtures/categorization' 7 require 'fixtures/company' 8 require 'fixtures/topic' 9 require 'fixtures/reply' 10 11 class LazyAssociationTest < Test::Unit::TestCase 12 fixtures :authors, :mixins, :companies, :posts, :topics 13 14 def test_lazy_association_loading 15 authors = Author.find(:all, :include=> :posts, :conditions => ['posts.type = ?', "Post"]) 16 assert_not_equal(0 , authors.length) 17 authors.each { |author| 18 assert author.send(:instance_variables).include? "@posts" } 19 authors = Author.find(:all, :include=> :posts, :lazy_associations => true, :conditions => ['posts.type = ?', "Post"]) 20 assert_not_equal(0 , authors.length) 21 # authors.each { |author| assert_queries {author.posts} } 22 authors.each { |author| 23 assert !(author.send(:instance_variables).include? "@posts") } 24 end 25 end 26 -
lib/active_record/associations.rb
old new 1115 1115 1116 1116 def find_with_associations(options = {}) 1117 1117 catch :invalid_query do 1118 join_dependency = JoinDependency.new(self, merge_includes(scope(:find, :include), options[:include]), options[:joins] )1118 join_dependency = JoinDependency.new(self, merge_includes(scope(:find, :include), options[:include]), options[:joins], options[:lazy_associations]) 1119 1119 rows = select_all_rows(options, join_dependency) 1120 1120 return join_dependency.instantiate(rows) 1121 1121 end … … 1369 1369 class JoinDependency # :nodoc: 1370 1370 attr_reader :joins, :reflections, :table_aliases 1371 1371 1372 def initialize(base, associations, joins )1372 def initialize(base, associations, joins, lazy_associations = false) 1373 1373 @joins = [JoinBase.new(base, joins)] 1374 1374 @associations = associations 1375 @lazy_associations = lazy_associations 1375 1376 @reflections = [] 1376 1377 @base_records_hash = {} 1377 1378 @base_records_in_order = [] … … 1394 1395 unless @base_records_hash[primary_id] 1395 1396 @base_records_in_order << (@base_records_hash[primary_id] = join_base.instantiate(row)) 1396 1397 end 1397 construct(@base_records_hash[primary_id], @associations, join_associations.dup, row) 1398 construct(@base_records_hash[primary_id], @associations, join_associations.dup, row) unless @lazy_associations 1398 1399 end 1399 1400 return @base_records_in_order 1400 1401 end -
lib/active_record/base.rb
old new 392 392 # * <tt>:readonly</tt>: Mark the returned records read-only so they cannot be saved or updated. 393 393 # * <tt>:lock</tt>: An SQL fragment like "FOR UPDATE" or "LOCK IN SHARE MODE". 394 394 # :lock => true gives connection's default exclusive lock, usually "FOR UPDATE". 395 # * <tt>:lazy_associations</tt> A boolean value, default is false. Normally when the :include option is used to add associated 396 # tables to the query, the cooresponding association collections will be created. This is normally desired, but it may 397 # result unnecessary processing and memory usage when the inclusion is solely to aid in selecting base records. 398 # If lazy_associations is true then the tables for any associations will participate in the sql query so that their fields can 399 # be used in the :conditions option, but the association collection will not be created by find. 395 400 # 396 401 # Examples for find by id: 397 402 # Person.find(1) # returns the object for ID = 1 … … 414 419 # Person.find(:all, :conditions => [ "category IN (?)", categories], :limit => 50) 415 420 # Person.find(:all, :offset => 10, :limit => 10) 416 421 # Person.find(:all, :include => [ :account, :friends ]) 422 # Person.find(:all, :include => :account, :lazy_associations => true, :conditions => ['accounts.last_accessed_time < ?', 1.year.ago]) 417 423 # Person.find(:all, :group => "category") 424 # 425 # Example for find_all: with :lazy_associations => true 418 426 # 427 # Person.find(:all, :include => :account, :lazy_associations => true, :conditions => ['accounts.last_accessed_time < ?', 1.year.ago]) 428 # 419 429 # Example for find with a lock. Imagine two concurrent transactions: 420 430 # each will read person.visits == 2, add 1 to it, and save, resulting 421 431 # in two saves of person.visits = 3. By locking the row, the second … … 1658 1668 end 1659 1669 end 1660 1670 1661 VALID_FIND_OPTIONS = [ :conditions, : include, :joins, :limit, :offset,1671 VALID_FIND_OPTIONS = [ :conditions, :lazy_associations, :include, :joins, :limit, :offset, 1662 1672 :order, :select, :readonly, :group, :from, :lock ] 1663 1673 1664 1674 def validate_find_options(options) #:nodoc: