Use Global Scopes
Say you want all your models to have a scope that lets you select them by date of their
- Make a module
# in /app/models/concerns/global_scopes.rb module GlobalScopes def self.included(base) base.class_eval do def self.in_daterange(start_date, end_date) all.where(created_at: start_date.to_date.beginning_of_day..end_date.to_date.end_of_day) end end end end
- Have the module included in ActiveRecord::Base.
# in /config/initializers/activerecord.rb ActiveRecord::Base.send(:include, GlobalScopes)
More at stackoverflow
Written on September 21, 2015