Skip to content

Instantly share code, notes, and snippets.

View haseebeqx's full-sized avatar
🟢
Building software

Haseeb Annadamban haseebeqx

🟢
Building software
View GitHub Profile
@haseebeqx
haseebeqx / pluck_in_batches.rb
Created July 14, 2024 05:44
Pluck in batches - combines the efficiency of pluck with the memory-friendly approach of find_in_batches
module ActiveRecord
class Relation
def pluck_in_batches(*columns, batch_size: 1000, start: nil)
relation = self
relation = relation.where(table[primary_key].gteq(start)) if start
batch_order = "#{table.name}.#{primary_key} ASC"
relation = relation.reorder(batch_order)
loop do
batch = relation.limit(batch_size).pluck(*columns, primary_key)
@justinweiss
justinweiss / filterable.rb
Last active January 30, 2025 13:06
Filterable
# Call scopes directly from your URL params:
#
# @products = Product.filter(params.slice(:status, :location, :starts_with))
module Filterable
extend ActiveSupport::Concern
module ClassMethods
# Call the class methods with names based on the keys in <tt>filtering_params</tt>
# with their associated values. For example, "{ status: 'delayed' }" would call