module ActiveRecord::SpawnMethods
def except(*skips)
Post.order('id asc').except(:order) # discards the order condition
Removes from the query the condition(s) specified in +skips+.
def except(*skips) relation_with values.except(*skips) end
def merge(other, *rest)
# => Post.where(published: true).joins(:comments)
Post.where(published: true).merge(-> { joins(:comments) })
Procs will be evaluated by merge:
# (This is just an example. You'd probably want to do this with a single query!)
# Returns the intersection of all published posts with the 5 most recently created posts.
Post.where(published: true).merge(recent_posts)
recent_posts = Post.order('created_at DESC').first(5)
# Performs a single join query with both where conditions.
Post.where(published: true).joins(:comments).merge( Comment.where(spam: false) )
Returns an array representing the intersection of the resulting records with other, if other is an array.
Merges in the conditions from other, if other is an ActiveRecord::Relation.
def merge(other, *rest) if other.is_a?(Array) records & other elsif other spawn.merge!(other, *rest) else raise ArgumentError, "invalid argument: #{other.inspect}." end end
def merge!(other, *rest) # :nodoc:
def merge!(other, *rest) # :nodoc: options = rest.extract_options! if other.is_a?(Hash) Relation::HashMerger.new(self, other, options[:rewhere]).merge elsif other.is_a?(Relation) Relation::Merger.new(self, other, options[:rewhere]).merge elsif other.respond_to?(:to_proc) instance_exec(&other) else raise ArgumentError, "#{other.inspect} is not an ActiveRecord::Relation" end end
def only(*onlies)
Post.order('id asc').only(:where) # discards the order condition
Removes any condition from the query other than the one(s) specified in +onlies+.
def only(*onlies) relation_with values.slice(*onlies) end
def relation_with(values)
def relation_with(values) result = spawn result.instance_variable_set(:@values, values) result end
def spawn # :nodoc:
This is overridden by Associations::CollectionProxy
def spawn # :nodoc: already_in_scope?(klass.scope_registry) ? klass.all : clone end