If you are using many times created_at and updated_at attributes and you’re
playing a lot with them in you queries, you should be interesting by this
MonkeyPatch who is
adding useful scopes in every models who inherits from ActiveRecord::Base.
MonkeyPatch
Create app/initializer/active_record_scopes_extension.rb file and add the
code below.
Call MyModel.created(DateTime.now) or MyModel.updated(3.days.ago) or
MyModel.created(2.day.ago, 1.day.ago).
12345678910111213141516171819202122
moduleScopesdefself.included(base)base.class_evaldodefself.created(date_start,date_end=nil)ifdate_start&&date_endscoped(:conditions=>["#{table_name}.created_at >= ? AND #{table_name}.created_at <= ?",date_start,date_end])elsifdate_startscoped(:conditions=>["#{table_name}.created_at >= ?",date_start])endenddefself.updated(date_start,date_end=nil)ifdate_start&&date_endscoped(:conditions=>["#{table_name}.updated_at >= ? AND #{table_name}.updated_at <= ?",date_start,date_end])elsifdate_startscoped(:conditions=>["#{table_name}.updated_at >= ?",date_start])endendendendendActiveRecord::Base.send(:include,Scopes)