[Feature Request]Add updated_before, updated_after, created_before, created_after functions for ActiveRecord objects.

Hi Guys,

It’s my first suggestion/post here so go easy on me :stuck_out_tongue:

So recently I’ve found myself writing scopes or functions like this:

class SomeModel < ActiveRecord::Base

scope :created_before, ->(timestamp) { where(‘created_at < ?’, timestamp) }

end

``

class SomeModel < ActiveRecord::Base

def self.updated_before(time)

where('updated_at < ?', time)

end

def self.updated_after(time)

where('updated_at > ?', time)

end

def self.updated_between(start_time, end_time)

updated_after(start_time).updated_before(end_time)

end

end

``

I would like to suggest the addition of these functions to ActiveRecord so all ActiveRecord Objects can inherit these functions (If time-stamps are enabled).

If this feature is something people are interested in, I’d love the opportunity to dev this as I want to contribute and give back to the Rails community.

Let me know what you guys think :slight_smile:

Will

Your updated_between can easily be accomplished by passing a date range to the where method. ie.start…start+1.day

Hey Will,

In Rails 5 all your models will inherit from ApplicationModel, which can then be the only place you’d need that code for all your models to use.

Thanks, Tom

Hey Tom,

Thanks for the suggestion, I’m currently using Rails 4.2.5 so I’m not familiar with Rails 5 and ApplicationModel.

I’ll look into upgrading to Rails 5 and implementing the idea in the ApplicationModel :slight_smile:

You can add an ApplicationModel to your rails 4 app as well. It’s just a new default base model in rails 5.