Module: ActiveRecord::Relation::QueryMethodsExt

Defined in:
lib/active_record/relation/query_methods_ext.rb

Instance Method Summary collapse

Instance Method Details

#force_index(index_name, direction: nil) ⇒ Object

Set table index hint for the query to the given ‘index_name`, and `direction` (either `ASC` or `DESC`).

Any call to ‘ActiveRecord::QueryMethods#from` will reset the index hint. Index hints are not set if the `from` clause is not a table name.



22
23
24
# File 'lib/active_record/relation/query_methods_ext.rb', line 22

def force_index(index_name, direction: nil)
  spawn.force_index!(index_name, direction: direction)
end

#force_index!(index_name, direction: nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/active_record/relation/query_methods_ext.rb', line 26

def force_index!(index_name, direction: nil)
  return self unless from_clause_is_a_table_name?

  index_name = sanitize_sql(index_name.to_s)
  direction = direction.to_s.upcase
  direction = %w[ASC DESC].include?(direction) ? ",#{direction}" : ""

  @force_index = "FORCE_INDEX=#{index_name}#{direction}"
  self.from_clause = build_from_clause_with_hints
  self
end

#from!Object

:nodoc:



6
7
8
9
10
# File 'lib/active_record/relation/query_methods_ext.rb', line 6

def from!(...) # :nodoc:
  @force_index = nil
  @index_hint = nil
  super
end

#index_hint(hint) ⇒ Object

Set table index hint for the query with the given ‘hint`. This allows more control over the hint than `ActiveRecord::Relation#force_index`. For instance, you could set it to `NO_FULL_SCAN`.

Any call to ‘ActiveRecord::QueryMethods#from` will reset the index hint. Index hints are not set if the `from` clause is not a table name.



49
50
51
# File 'lib/active_record/relation/query_methods_ext.rb', line 49

def index_hint(hint)
  spawn.index_hint!(hint)
end

#index_hint!(hint) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/active_record/relation/query_methods_ext.rb', line 53

def index_hint!(hint)
  return self unless from_clause_is_a_table_name?

  hint = sanitize_sql(hint.to_s)
  @index_hint = hint.to_s
  self.from_clause = build_from_clause_with_hints
  self
end