Module: ActiveRecord::QueryMethods

Extended by:
ActiveSupport::Concern
Included in:
Relation
Defined in:
activerecord/lib/active_record/relation/query_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ActiveSupport::Concern

append_features, extended, included

Instance Attribute Details

#bind_valuesObject

Returns the value of attribute bind_values



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def bind_values
  @bind_values
end

#create_with_valueObject

Returns the value of attribute create_with_value



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def create_with_value
  @create_with_value
end

#eager_load_valuesObject

Returns the value of attribute eager_load_values



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def eager_load_values
  @eager_load_values
end

#from_valueObject

Returns the value of attribute from_value



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def from_value
  @from_value
end

#group_valuesObject

Returns the value of attribute group_values



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def group_values
  @group_values
end

#having_valuesObject

Returns the value of attribute having_values



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def having_values
  @having_values
end

#includes_valuesObject

Returns the value of attribute includes_values



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def includes_values
  @includes_values
end

#joins_valuesObject

Returns the value of attribute joins_values



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def joins_values
  @joins_values
end

#limit_valueObject

Returns the value of attribute limit_value



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def limit_value
  @limit_value
end

#lock_valueObject

Returns the value of attribute lock_value



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def lock_value
  @lock_value
end

#offset_valueObject

Returns the value of attribute offset_value



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def offset_value
  @offset_value
end

#order_valuesObject

Returns the value of attribute order_values



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def order_values
  @order_values
end

#preload_valuesObject

Returns the value of attribute preload_values



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def preload_values
  @preload_values
end

#readonly_valueObject

Returns the value of attribute readonly_value



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def readonly_value
  @readonly_value
end

#reorder_valueObject

Returns the value of attribute reorder_value



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def reorder_value
  @reorder_value
end

#reverse_order_valueObject

Returns the value of attribute reverse_order_value



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def reverse_order_value
  @reverse_order_value
end

#select_valuesObject

Returns the value of attribute select_values



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def select_values
  @select_values
end

#where_valuesObject

Returns the value of attribute where_values



8
9
10
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 8

def where_values
  @where_values
end

Instance Method Details

#arelObject



195
196
197
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 195

def arel
  @arel ||= with_default_scope.build_arel
end

#bind(value) ⇒ Object



114
115
116
117
118
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 114

def bind(value)
  relation = clone
  relation.bind_values += [value]
  relation
end

#build_arelObject



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 199

def build_arel
  arel = table.from table

  build_joins(arel, @joins_values) unless @joins_values.empty?

  collapse_wheres(arel, (@where_values - ['']).uniq)

  arel.having(*@having_values.uniq.reject{|h| h.blank?}) unless @having_values.empty?

  arel.take(connection.sanitize_limit(@limit_value)) if @limit_value
  arel.skip(@offset_value) if @offset_value

  arel.group(*@group_values.uniq.reject{|g| g.blank?}) unless @group_values.empty?

  order = @reorder_value ? @reorder_value : @order_values
  order = reverse_sql_order(order) if @reverse_order_value
  arel.order(*order.uniq.reject{|o| o.blank?}) unless order.empty?

  build_select(arel, @select_values.uniq)

  arel.from(@from_value) if @from_value
  arel.lock(@lock_value) if @lock_value

  arel
end

#create_with(value) ⇒ Object



167
168
169
170
171
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 167

def create_with(value)
  relation = clone
  relation.create_with_value = value ? create_with_value.merge(value) : {}
  relation
end

#eager_load(*args) ⇒ Object



24
25
26
27
28
29
30
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 24

def eager_load(*args)
  return self if args.blank?

  relation = clone
  relation.eager_load_values += args
  relation
end

#extending(*modules) ⇒ Object



179
180
181
182
183
184
185
186
187
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 179

def extending(*modules)
  modules << Module.new(&Proc.new) if block_given?

  return self if modules.empty?

  relation = clone
  relation.send(:apply_modules, modules.flatten)
  relation
end

#from(value) ⇒ Object



173
174
175
176
177
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 173

def from(value)
  relation = clone
  relation.from_value = value
  relation
end

#group(*args) ⇒ Object



79
80
81
82
83
84
85
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 79

def group(*args)
  return self if args.blank?

  relation = clone
  relation.group_values += args.flatten
  relation
end

#having(opts, *rest) ⇒ Object



128
129
130
131
132
133
134
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 128

def having(opts, *rest)
  return self if opts.blank?

  relation = clone
  relation.having_values += build_where(opts, rest)
  relation
end

#includes(*args) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 14

def includes(*args)
  args.reject! {|a| a.blank? }

  return self if args.empty?

  relation = clone
  relation.includes_values = (relation.includes_values + args).flatten.uniq
  relation
end

#joins(*args) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 103

def joins(*args)
  return self if args.compact.blank?

  relation = clone

  args.flatten!
  relation.joins_values += args

  relation
end

#limit(value) ⇒ Object



136
137
138
139
140
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 136

def limit(value)
  relation = clone
  relation.limit_value = value
  relation
end

#lock(locks = true) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 148

def lock(locks = true)
  relation = clone

  case locks
  when String, TrueClass, NilClass
    relation.lock_value = locks || true
  else
    relation.lock_value = false
  end

  relation
end

#offset(value) ⇒ Object



142
143
144
145
146
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 142

def offset(value)
  relation = clone
  relation.offset_value = value
  relation
end

#order(*args) ⇒ Object



87
88
89
90
91
92
93
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 87

def order(*args)
  return self if args.blank?

  relation = clone
  relation.order_values += args.flatten
  relation
end

#preload(*args) ⇒ Object



32
33
34
35
36
37
38
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 32

def preload(*args)
  return self if args.blank?

  relation = clone
  relation.preload_values += args
  relation
end

#readonly(value = true) ⇒ Object



161
162
163
164
165
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 161

def readonly(value = true)
  relation = clone
  relation.readonly_value = value
  relation
end

#reorder(*args) ⇒ Object



95
96
97
98
99
100
101
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 95

def reorder(*args)
  return self if args.blank?

  relation = clone
  relation.reorder_value = args.flatten
  relation
end

#reverse_orderObject



189
190
191
192
193
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 189

def reverse_order
  relation = clone
  relation.reverse_order_value = !relation.reverse_order_value
  relation
end

#select(value = Proc.new) ⇒ Object

Works in two unique ways.

First: takes a block so it can be used just like Array#select.

Model.scoped.select { |m| m.field == value }

This will build an array of objects from the database for the scope, converting them into an array and iterating through them using Array#select.

Second: Modifies the SELECT statement for the query so that only certain fields are retrieved:

>> Model.select(:field)
=> [#<Model field:value>]

Although in the above example it looks as though this method returns an array, it actually returns a relation object and can have other query methods appended to it, such as the other methods in ActiveRecord::QueryMethods.

This method will also take multiple parameters:

>> Model.select(:field, :other_field, :and_one_more)
=> [#<Model field: "value", other_field: "value", and_one_more: "value">]

Any attributes that do not have fields retrieved by a select will return `nil` when the getter method for that attribute is used:

>> Model.select(:field).first.other_field
=> nil


69
70
71
72
73
74
75
76
77
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 69

def select(value = Proc.new)
  if block_given?
    to_a.select {|*block_args| value.call(*block_args) }
  else
    relation = clone
    relation.select_values += Array.wrap(value)
    relation
  end
end

#where(opts, *rest) ⇒ Object



120
121
122
123
124
125
126
# File 'activerecord/lib/active_record/relation/query_methods.rb', line 120

def where(opts, *rest)
  return self if opts.blank?

  relation = clone
  relation.where_values += build_where(opts, rest)
  relation
end