Hi, this blog is no longer maintained, my new blog is here

Ruby On Rails and a Conning Israeli entrepreneur

Rails date Range

Holy Shmoly! Sometimes Rails and ruby just blows me away,

Let’s assume you are trying to find a bunch of records between a time frame.


class Call < ActiveRecord::Base
named_scope :by_month, lambda { |d| { :conditions => { :date => d.beginning_of_month..d.end_of_month } } }
end


simply specify a range

and running (the named_scope )


this_months_calls = Call.by_month Date.today

And Rails will return all the calls that were recorded during the current month. Love it.

5 comments:

  Jerome

November 30, 2008 at 5:40 PM

named_scope :by_month, lambda { |d| { :conditions => ["MONTH(date) = ?", d.month ] } }

(not applicable to postgresql but damn faster)

  Elad Meidar

November 30, 2008 at 10:24 PM

yeah well, i never really liked you :)
i was never a fan of skinny Frenchmen, nor optimization :)

  Jerome

December 1, 2008 at 8:21 PM

Damn I like to kick your fat ass

  Alexey

December 7, 2008 at 10:01 AM

lol :)

go, guyes go !!

DM !!

i always loves urs counterstrikes :)

  dasil003

March 1, 2009 at 6:08 PM

@Jerome: Too bad that doesn't actually select a single month, it selects that month from all years. Fail!


The Web Ask eizesus.com

Subscribe

    follow me on Twitter

    Twiters Around

    About Me

    My photo
    I am a web developer for more than 9 years, managed, cried, coded, designed and made money in this industry. now trying to do it again.

    Labels