Is there a ruby ​​/ rail pearl that makes viewing easier

Does anyone know a gem that facilitates review dates? I would like to make Model.this_year (: created_at) .where () ... etc. The thought of what I once heard about this.

I thought something like dues_paid_this_year or updated_at_two_hours_ago strings. A gem that could do this, like search_logic, creating dynamic regions would make my world beautiful.

+4
source share
2 answers

I do not know about stone, although it would be great to create it. In this case, you will probably receive:

class Thing < ActiveRecord::Base # Some date-related scopes scope :today, where("created_at > ?", Time.now.beginning_of_day) scope :yesterday, where("created_at < ? and created_at > ?", 1.day.ago.end_of_day, 1.day.ago.beginning_of_day) scope :this_month, where("created_at > ?", Time.now.beginning_of_month) scope :last_month, where("created_at < ? and created_at > ?", 1.month.ago.end_of_month, 1.month.ago.beginning_of_month) scope :this_year, where("created_at > ?", Time.now.beginning_of_year) ### # All your model stuff goes here ### # Let get real tricky with method_missing # and allow Thing.last_X_days for work for any value of X private def self.method_missing(method, *args, &block) if method.to_s =~ /^last_(\d+)_days$/i days = method.to_s.match(/^last_(\d+)_days$/i) return self.where("created_at > ?", days[1].to_i.days.ago.beginning_of_day) else super end end end 

This will give you some basic areas, such as Thing.this_month ... plus a dynamic finder that will allow you to do something like Thing.last_90_days and work for any number (WARNING: I have a little method_missing n00b, the code worked for me, but, maybe someone can check it twice).

FYI I accept Rails 3 here.

+2
source

You can't do what you want with lambda regions?

0
source

All Articles