Skip to content

Instantly share code, notes, and snippets.

@dcyoung-dev
Created Apr 28, 2021
Embed
What would you like to do?
class PostsQuery < ActiveRecord::Base
self.table_name = "posts"
belongs_to :user
def self.report_filtering(filters)
filters.inject(self) do |klass, (method_name, args)|
klass.try("by_#{method_name}", args) || klass.current_scope
end
end
def self.by_users(users)
return current_scope unless users.present?
joins(:user).where(users: users)
end
def self.by_created_at_start_date(created_at_start_date)
return current_scope unless created_at_start_date.present?
where(created_at: created_at_start_date..)
end
end
puts PostsQuery.report_filtering(
users: [User.find(1), User.find(5)],
created_at_start_date: Date.current,
created_at_end_date: Date.current,
updated_at_start_date: Date.current,
updated_at_end_date: Date.current,
).to_sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment