Skip to content

Instantly share code, notes, and snippets.

@eprothro
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eprothro/dc022de04c58291d5430 to your computer and use it in GitHub Desktop.
Save eprothro/dc022de04c58291d5430 to your computer and use it in GitHub Desktop.
QueryObject as Decorator for relation
class TestQuery < SimpleDelegator
  def initialize
    super(Bomb.all.extending(Scopes))
  end
  module Scopes
    def processed
      where(Bomb.arel_table[:asset_remote_url].eq(nil))
    end
  end
end
q = TestQuery.new 

q.__getobj__.class    # <= q is a decorated Active Record Association Relation
=> Bomb::ActiveRecord_Relation

q.to_sql
=> "SELECT \"bombs\".* FROM \"bombs\""

q.processed.to_sql
=> "SELECT \"bombs\".* FROM \"bombs\" WHERE \"bombs\".\"asset_remote_url\" IS NULL"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment