Skip to content

Instantly share code, notes, and snippets.

@parasquid
Last active August 29, 2015 14:24
Show Gist options
  • Save parasquid/58583489732d65a9be9d to your computer and use it in GitHub Desktop.
Save parasquid/58583489732d65a9be9d to your computer and use it in GitHub Desktop.
A good query object template
class OrderLinesFromUnitedStatesQuery
class << self
delegate :call, to: :new
end
def initialize(relation = OrderLine.all)
@relation = relation
end
def call
@relation.joins(:order => :billing_address).where('addresses.country' => 'United States')
end
end
# example usage:
OrderLinesFromUnitedStatesQuery.call.find_each do |ol|
puts "processing orderline id #{ol.id}"
# ...
end
# will also allow this:
class OrderLine < ActiveRecord::Base
scope :from_united_states, OrderLinesFromUnitedStatesQuery
end
OrderLine.from_united_states.find_each do |ol|
# processing here
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment