# File lib/active_record/relation/spawn_methods.rb, line 128 128: def apply_finder_options(options) 129: relation = clone 130: return relation unless options 131: 132: options.assert_valid_keys(VALID_FIND_OPTIONS) 133: finders = options.dup 134: finders.delete_if { |key, value| value.nil? && key != :limit } 135: 136: ([:joins, :select, :group, :order, :having, :limit, :offset, :from, :lock, :readonly] & finders.keys).each do |finder| 137: relation = relation.send(finder, finders[finder]) 138: end 139: 140: relation = relation.where(finders[:conditions]) if options.has_key?(:conditions) 141: relation = relation.includes(finders[:include]) if options.has_key?(:include) 142: relation = relation.extending(finders[:extend]) if options.has_key?(:extend) 143: 144: relation 145: end
Removes from the query the condition(s) specified in skips.
Example:
Post.order('id asc').except(:order) # discards the order condition Post.where('id > 10').order('id asc').except(:where) # discards the where condition but keeps the order
# File lib/active_record/relation/spawn_methods.rb, line 82 82: def except(*skips) 83: result = self.class.new(@klass, table) 84: result.default_scoped = default_scoped 85: 86: ((Relation::ASSOCIATION_METHODS + Relation::MULTI_VALUE_METHODS) - skips).each do |method| 87: result.send(:"#{method}_values=", send(:"#{method}_values")) 88: end 89: 90: (Relation::SINGLE_VALUE_METHODS - skips).each do |method| 91: result.send(:"#{method}_value=", send(:"#{method}_value")) 92: end 93: 94: # Apply scope extension modules 95: result.send(:apply_modules, extensions) 96: 97: result 98: end
# File lib/active_record/relation/spawn_methods.rb, line 5 5: def merge(r) 6: return self unless r 7: return to_a & r if r.is_a?(Array) 8: 9: merged_relation = clone 10: 11: r = r.with_default_scope if r.default_scoped? && r.klass != klass 12: 13: Relation::ASSOCIATION_METHODS.each do |method| 14: value = r.send(:"#{method}_values") 15: 16: unless value.empty? 17: if method == :includes 18: merged_relation = merged_relation.includes(value) 19: else 20: merged_relation.send(:"#{method}_values=", value) 21: end 22: end 23: end 24: 25: (Relation::MULTI_VALUE_METHODS - [:joins, :where, :order]).each do |method| 26: value = r.send(:"#{method}_values") 27: merged_relation.send(:"#{method}_values=", merged_relation.send(:"#{method}_values") + value) if value.present? 28: end 29: 30: merged_relation.joins_values += r.joins_values 31: 32: merged_wheres = @where_values + r.where_values 33: 34: unless @where_values.empty? 35: # Remove duplicates, last one wins. 36: seen = Hash.new { |h,table| h[table] = {} } 37: merged_wheres = merged_wheres.reverse.reject { |w| 38: nuke = false 39: if w.respond_to?(:operator) && w.operator == :== 40: name = w.left.name 41: table = w.left.relation.name 42: nuke = seen[table][name] 43: seen[table][name] = true 44: end 45: nuke 46: }.reverse 47: end 48: 49: merged_relation.where_values = merged_wheres 50: 51: (Relation::SINGLE_VALUE_METHODS - [:lock, :create_with, :reordering]).each do |method| 52: value = r.send(:"#{method}_value") 53: merged_relation.send(:"#{method}_value=", value) unless value.nil? 54: end 55: 56: merged_relation.lock_value = r.lock_value unless merged_relation.lock_value 57: 58: merged_relation = merged_relation.create_with(r.create_with_value) unless r.create_with_value.empty? 59: 60: if (r.reordering_value) 61: # override any order specified in the original relation 62: merged_relation.reordering_value = true 63: merged_relation.order_values = r.order_values 64: else 65: # merge in order_values from r 66: merged_relation.order_values += r.order_values 67: end 68: 69: # Apply scope extension modules 70: merged_relation.send :apply_modules, r.extensions 71: 72: merged_relation 73: end
Removes any condition from the query other than the one(s) specified in onlies.
Example:
Post.order('id asc').only(:where) # discards the order condition Post.order('id asc').only(:where, :order) # uses the specified order
# File lib/active_record/relation/spawn_methods.rb, line 107 107: def only(*onlies) 108: result = self.class.new(@klass, table) 109: result.default_scoped = default_scoped 110: 111: ((Relation::ASSOCIATION_METHODS + Relation::MULTI_VALUE_METHODS) & onlies).each do |method| 112: result.send(:"#{method}_values=", send(:"#{method}_values")) 113: end 114: 115: (Relation::SINGLE_VALUE_METHODS & onlies).each do |method| 116: result.send(:"#{method}_value=", send(:"#{method}_value")) 117: end 118: 119: # Apply scope extension modules 120: result.send(:apply_modules, extensions) 121: 122: result 123: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.