Last active
August 29, 2015 14:24
-
-
Save parasquid/58583489732d65a9be9d to your computer and use it in GitHub Desktop.
A good query object template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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