Files

Class Index [+]

Quicksearch

ActiveRecord::QueryMethods

Attributes

includes_values[RW]
eager_load_values[RW]
preload_values[RW]
select_values[RW]
group_values[RW]
order_values[RW]
joins_values[RW]
where_values[RW]
having_values[RW]
bind_values[RW]
limit_value[RW]
offset_value[RW]
lock_value[RW]
readonly_value[RW]
create_with_value[RW]
from_value[RW]
reordering_value[RW]
reverse_order_value[RW]
uniq_value[RW]

Public Instance Methods

arel() click to toggle source
     # File lib/active_record/relation/query_methods.rb, line 259
259:     def arel
260:       @arel ||= with_default_scope.build_arel
261:     end
bind(value) click to toggle source
     # File lib/active_record/relation/query_methods.rb, line 126
126:     def bind(value)
127:       relation = clone
128:       relation.bind_values += [value]
129:       relation
130:     end
build_arel() click to toggle source
     # File lib/active_record/relation/query_methods.rb, line 263
263:     def build_arel
264:       arel = table.from table
265: 
266:       build_joins(arel, @joins_values) unless @joins_values.empty?
267: 
268:       collapse_wheres(arel, (@where_values - ['']).uniq)
269: 
270:       arel.having(*@having_values.uniq.reject{|h| h.blank?}) unless @having_values.empty?
271: 
272:       arel.take(connection.sanitize_limit(@limit_value)) if @limit_value
273:       arel.skip(@offset_value.to_i) if @offset_value
274: 
275:       arel.group(*@group_values.uniq.reject{|g| g.blank?}) unless @group_values.empty?
276: 
277:       order = @order_values
278:       order = reverse_sql_order(order) if @reverse_order_value
279:       arel.order(*order.uniq.reject{|o| o.blank?}) unless order.empty?
280: 
281:       build_select(arel, @select_values.uniq)
282: 
283:       arel.distinct(@uniq_value)
284:       arel.from(@from_value) if @from_value
285:       arel.lock(@lock_value) if @lock_value
286: 
287:       arel
288:     end
create_with(value) click to toggle source
     # File lib/active_record/relation/query_methods.rb, line 179
179:     def create_with(value)
180:       relation = clone
181:       relation.create_with_value = value ? create_with_value.merge(value) : {}
182:       relation
183:     end
eager_load(*args) click to toggle source
    # File lib/active_record/relation/query_methods.rb, line 25
25:     def eager_load(*args)
26:       return self if args.blank?
27: 
28:       relation = clone
29:       relation.eager_load_values += args
30:       relation
31:     end
extending(*modules) click to toggle source

Used to extend a scope with additional methods, either through a module or through a block provided.

The object returned is a relation, which can be further extended.

Using a module

  module Pagination
    def page(number)
      # pagination code goes here
    end
  end

  scope = Model.scoped.extending(Pagination)
  scope.page(params[:page])

You can also pass a list of modules:

  scope = Model.scoped.extending(Pagination, SomethingElse)

Using a block

  scope = Model.scoped.extending do
    def page(number)
      # pagination code goes here
    end
  end
  scope.page(params[:page])

You can also use a block and a module list:

  scope = Model.scoped.extending(Pagination) do
    def per_page(number)
      # pagination code goes here
    end
  end
     # File lib/active_record/relation/query_methods.rb, line 243
243:     def extending(*modules)
244:       modules << Module.new(&Proc.new) if block_given?
245: 
246:       return self if modules.empty?
247: 
248:       relation = clone
249:       relation.send(:apply_modules, modules.flatten)
250:       relation
251:     end
from(value) click to toggle source
     # File lib/active_record/relation/query_methods.rb, line 185
185:     def from(value)
186:       relation = clone
187:       relation.from_value = value
188:       relation
189:     end
group(*args) click to toggle source
    # File lib/active_record/relation/query_methods.rb, line 80
80:     def group(*args)
81:       return self if args.blank?
82: 
83:       relation = clone
84:       relation.group_values += args.flatten
85:       relation
86:     end
having(opts, *rest) click to toggle source
     # File lib/active_record/relation/query_methods.rb, line 140
140:     def having(opts, *rest)
141:       return self if opts.blank?
142: 
143:       relation = clone
144:       relation.having_values += build_where(opts, rest)
145:       relation
146:     end
includes(*args) click to toggle source
    # File lib/active_record/relation/query_methods.rb, line 15
15:     def includes(*args)
16:       args.reject! {|a| a.blank? }
17: 
18:       return self if args.empty?
19: 
20:       relation = clone
21:       relation.includes_values = (relation.includes_values + args).flatten.uniq
22:       relation
23:     end
joins(*args) click to toggle source
     # File lib/active_record/relation/query_methods.rb, line 115
115:     def joins(*args)
116:       return self if args.compact.blank?
117: 
118:       relation = clone
119: 
120:       args.flatten!
121:       relation.joins_values += args
122: 
123:       relation
124:     end
limit(value) click to toggle source
     # File lib/active_record/relation/query_methods.rb, line 148
148:     def limit(value)
149:       relation = clone
150:       relation.limit_value = value
151:       relation
152:     end
lock(locks = true) click to toggle source
     # File lib/active_record/relation/query_methods.rb, line 160
160:     def lock(locks = true)
161:       relation = clone
162: 
163:       case locks
164:       when String, TrueClass, NilClass
165:         relation.lock_value = locks || true
166:       else
167:         relation.lock_value = false
168:       end
169: 
170:       relation
171:     end
offset(value) click to toggle source
     # File lib/active_record/relation/query_methods.rb, line 154
154:     def offset(value)
155:       relation = clone
156:       relation.offset_value = value
157:       relation
158:     end
order(*args) click to toggle source
    # File lib/active_record/relation/query_methods.rb, line 88
88:     def order(*args)
89:       return self if args.blank?
90: 
91:       relation = clone
92:       relation.order_values += args.flatten
93:       relation
94:     end
preload(*args) click to toggle source
    # File lib/active_record/relation/query_methods.rb, line 33
33:     def preload(*args)
34:       return self if args.blank?
35: 
36:       relation = clone
37:       relation.preload_values += args
38:       relation
39:     end
readonly(value = true) click to toggle source
     # File lib/active_record/relation/query_methods.rb, line 173
173:     def readonly(value = true)
174:       relation = clone
175:       relation.readonly_value = value
176:       relation
177:     end
reorder(*args) click to toggle source

Replaces any existing order defined on the relation with the specified order.

  User.order('email DESC').reorder('id ASC') # generated SQL has 'ORDER BY id ASC'

Subsequent calls to order on the same relation will be appended. For example:

  User.order('email DESC').reorder('id ASC').order('name ASC')

generates a query with ‘ORDER BY id ASC, name ASC’.

     # File lib/active_record/relation/query_methods.rb, line 106
106:     def reorder(*args)
107:       return self if args.blank?
108: 
109:       relation = clone
110:       relation.reordering_value = true
111:       relation.order_values = args.flatten
112:       relation
113:     end
reverse_order() click to toggle source
     # File lib/active_record/relation/query_methods.rb, line 253
253:     def reverse_order
254:       relation = clone
255:       relation.reverse_order_value = !relation.reverse_order_value
256:       relation
257:     end
select(value = Proc.new) click to toggle source

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.

The argument to the method can also be an array of fields.

  >> 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 raise a ActiveModel::MissingAttributeError when the getter method for that attribute is used:

  >> Model.select(:field).first.other_field
  => ActiveModel::MissingAttributeError: missing attribute: other_field
    # File lib/active_record/relation/query_methods.rb, line 70
70:     def select(value = Proc.new)
71:       if block_given?
72:         to_a.select {|*block_args| value.call(*block_args) }
73:       else
74:         relation = clone
75:         relation.select_values += Array.wrap(value)
76:         relation
77:       end
78:     end
uniq(value = true) click to toggle source

Specifies whether the records should be unique or not. For example:

  User.select(:name)
  # => Might return two records with the same name

  User.select(:name).uniq
  # => Returns 1 record per unique name

  User.select(:name).uniq.uniq(false)
  # => You can also remove the uniqueness
     # File lib/active_record/relation/query_methods.rb, line 201
201:     def uniq(value = true)
202:       relation = clone
203:       relation.uniq_value = value
204:       relation
205:     end
where(opts, *rest) click to toggle source
     # File lib/active_record/relation/query_methods.rb, line 132
132:     def where(opts, *rest)
133:       return self if opts.blank?
134: 
135:       relation = clone
136:       relation.where_values += build_where(opts, rest)
137:       relation
138:     end

Private Instance Methods

apply_modules(modules) click to toggle source
     # File lib/active_record/relation/query_methods.rb, line 387
387:     def apply_modules(modules)
388:       unless modules.empty?
389:         @extensions += modules
390:         modules.each {|extension| extend(extension) }
391:       end
392:     end
array_of_strings?(o) click to toggle source
     # File lib/active_record/relation/query_methods.rb, line 412
412:     def array_of_strings?(o)
413:       o.is_a?(Array) && o.all?{|obj| obj.is_a?(String)}
414:     end
build_joins(manager, joins) click to toggle source
     # File lib/active_record/relation/query_methods.rb, line 333
333:     def build_joins(manager, joins)
334:       buckets = joins.group_by do |join|
335:         case join
336:         when String
337:           'string_join'
338:         when Hash, Symbol, Array
339:           'association_join'
340:         when ActiveRecord::Associations::JoinDependency::JoinAssociation
341:           'stashed_join'
342:         when Arel::Nodes::Join
343:           'join_node'
344:         else
345:           raise 'unknown class: %s' % join.class.name
346:         end
347:       end
348: 
349:       association_joins         = buckets['association_join'] || []
350:       stashed_association_joins = buckets['stashed_join'] || []
351:       join_nodes                = (buckets['join_node'] || []).uniq
352:       string_joins              = (buckets['string_join'] || []).map { |x|
353:         x.strip
354:       }.uniq
355: 
356:       join_list = join_nodes + custom_join_ast(manager, string_joins)
357: 
358:       join_dependency = ActiveRecord::Associations::JoinDependency.new(
359:         @klass,
360:         association_joins,
361:         join_list
362:       )
363: 
364:       join_dependency.graft(*stashed_association_joins)
365: 
366:       @implicit_readonly = true unless association_joins.empty? && stashed_association_joins.empty?
367: 
368:       # FIXME: refactor this to build an AST
369:       join_dependency.join_associations.each do |association|
370:         association.join_to(manager)
371:       end
372: 
373:       manager.join_sources.concat join_list
374: 
375:       manager
376:     end
build_select(arel, selects) click to toggle source
     # File lib/active_record/relation/query_methods.rb, line 378
378:     def build_select(arel, selects)
379:       unless selects.empty?
380:         @implicit_readonly = false
381:         arel.project(*selects)
382:       else
383:         arel.project(@klass.arel_table[Arel.star])
384:       end
385:     end
build_where(opts, other = []) click to toggle source
     # File lib/active_record/relation/query_methods.rb, line 321
321:     def build_where(opts, other = [])
322:       case opts
323:       when String, Array
324:         [@klass.send(:sanitize_sql, other.empty? ? opts : ([opts] + other))]
325:       when Hash
326:         attributes = @klass.send(:expand_hash_conditions_for_aggregates, opts)
327:         PredicateBuilder.build_from_hash(table.engine, attributes, table)
328:       else
329:         [opts]
330:       end
331:     end
collapse_wheres(arel, wheres) click to toggle source
     # File lib/active_record/relation/query_methods.rb, line 310
310:     def collapse_wheres(arel, wheres)
311:       equalities = wheres.grep(Arel::Nodes::Equality)
312: 
313:       arel.where(Arel::Nodes::And.new(equalities)) unless equalities.empty?
314: 
315:       (wheres - equalities).each do |where|
316:         where = Arel.sql(where) if String === where
317:         arel.where(Arel::Nodes::Grouping.new(where))
318:       end
319:     end
custom_join_ast(table, joins) click to toggle source
     # File lib/active_record/relation/query_methods.rb, line 292
292:     def custom_join_ast(table, joins)
293:       joins = joins.reject { |join| join.blank? }
294: 
295:       return [] if joins.empty?
296: 
297:       @implicit_readonly = true
298: 
299:       joins.map do |join|
300:         case join
301:         when Array
302:           join = Arel.sql(join.join(' ')) if array_of_strings?(join)
303:         when String
304:           join = Arel.sql(join)
305:         end
306:         table.create_string_join(join)
307:       end
308:     end
reverse_sql_order(order_query) click to toggle source
     # File lib/active_record/relation/query_methods.rb, line 394
394:     def reverse_sql_order(order_query)
395:       order_query = ["#{quoted_table_name}.#{quoted_primary_key} ASC"] if order_query.empty?
396: 
397:       order_query.map do |o|
398:         case o
399:         when Arel::Nodes::Ordering
400:           o.reverse
401:         when String, Symbol
402:           o.to_s.split(',').collect do |s|
403:             s.strip!
404:             s.gsub!(/\sasc\Z/, ' DESC') || s.gsub!(/\sdesc\Z/, ' ASC') || s.concat(' DESC')
405:           end
406:         else
407:           o
408:         end
409:       end.flatten
410:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.