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

29 views
Skip to first unread message

Will Hennessey

unread,
Jul 21, 2016, 5:06:23 PM7/21/16
to Ruby on Rails: Core
Hi Guys,

It's my first suggestion/post here so go easy on me :p 
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 :)

Will

Andrew Kaspick

unread,
Jul 21, 2016, 5:15:35 PM7/21/16
to Ruby on Rails: Core

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


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Tom Prats

unread,
Jul 21, 2016, 8:01:25 PM7/21/16
to Ruby on Rails: Core
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

Will Hennessey

unread,
Jul 22, 2016, 4:18:03 AM7/22/16
to Ruby on Rails: Core
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 :)

Andrew Kaspick

unread,
Jul 22, 2016, 8:50:24 AM7/22/16
to Ruby on Rails: Core

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

Reply all
Reply to author
Forward
0 new messages