Module: ActiveRecord::Deferrable::SingletonMethods

Defined in:
lib/active_record/connection_adapters/mysqlplus_adapter/deferrable/macro.rb

Instance Method Summary collapse

Instance Method Details

#find_by_sql_with_defer(sql, defer = false) ⇒ Object

Execute raw SQL in another Thread.Blocks only when the result is immediately referenced. … Record.find_by_sql( “SELECT SLEEP (1)”, true ) …



72
73
74
75
76
77
78
79
80
# File 'lib/active_record/connection_adapters/mysqlplus_adapter/deferrable/macro.rb', line 72

def find_by_sql_with_defer( sql, defer = false )
  if defer
    ActiveRecord::Deferrable::Result.new do 
      find_by_sql_without_defer( sql )
    end
  else
    find_by_sql_without_defer( sql )
  end    
end

#find_with_defer(*args) ⇒ Object

Executes a query in another background Thread.Blocks only when the result is immediately referenced. … Record.find( :first, :conditions => [‘records.some_id >= ?’, 100], :defer => true ) …



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/active_record/connection_adapters/mysqlplus_adapter/deferrable/macro.rb', line 88

def find_with_defer( *args )
  options = args.dup.extract_options!
  if options.key?(:defer)
    with_deferred_scope do
      ActiveRecord::Deferrable::Result.new do 
        find_without_defer(*args)
      end
    end  
  else
    find_without_defer(*args)
  end    
end

#preload_associations_with_defer(records, associations, preload_options = {}) ⇒ Object

!! EXPERIMENTAL !! Since ActiveRecord 2.1, multiple lightweight queries is preferred to expensive JOINS when eager loading related models.In some use cases it’s more performant to distribute those over multiple connections versus dispatching them on the same connection.

.… Record.find(:first, :include => [:other, :another], :defer => true) .…



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/active_record/connection_adapters/mysqlplus_adapter/deferrable/macro.rb', line 42

def preload_associations_with_defer(records, associations, preload_options={})
  if preload_deferred?( associations )        
    associations.delete(:defer)
    records = [records].flatten.compact.uniq
    return if records.empty?
    case associations
    when Array then associations.each {|association| ActiveRecord::Deferrable::Result.new{ preload_associations_without_defer(records, association, preload_options) } }
    when Symbol, String then ActiveRecord::Deferrable::Result.new{ preload_one_association(records, associations.to_sym, preload_options) }
    when Hash then
      associations.each do |parent, child|
        raise "parent must be an association name" unless parent.is_a?(String) || parent.is_a?(Symbol)
        preload_associations(records, parent, preload_options)
        reflection = reflections[parent]
        parents = records.map {|record| record.send(reflection.name)}.flatten.compact
        unless parents.empty?
          parents.first.class.preload_associations(parents, child)
        end
      end
    end
  else
    preload_associations_without_defer(records, associations, preload_options)
  end    
end