ActiveRecord: selecting date range of records

I have a simple timeclock application with an Admin page. I’m trying to
find an easy way to select a range of record from the database. For
instance “This week”, “Last week”, or “Last month” without having to
manually calculate the dates. This is what I’ve come up with, but it
does require me to manually calculate everything:

startFrame = “2007-09-18”
endFrame = “2007-09-20”

@punches = Punch.find(:all, :conditions => [‘timein > :startFrame AND
timein < :endFrame’, {:startFrame => startFrame, :endFrame =>
endFrame}])

The variables are there so eventually the user can select their own date
range.

Hey Jason, please remember that the console is your friend. Thus, you
can
simply test it out in the console:

@punches = Punch.find(:all, :conditions => [ ‘timein >= ? AND timein <=
?’,
startFrame, endFrame ] )

Good luck,

-Conrad

Thanks for the reply. The selection code is working fine. What I’m
trying to avoid is having to manually calculate the values of startFrame
and endFrame. Is there a method or something in rails to just say select
all record within the last week but not prior to Sunday? So if it is
Wednesday today I would get the records for Monday, Tuesday, and
Wednesday.

I know I can calculate the values with a bunch of code-fu but I’d like
to avoid it if I can.

Jason B. wrote:

Thanks for the reply. The selection code is working fine. What I’m
trying to avoid is having to manually calculate the values of startFrame
and endFrame. Is there a method or something in rails to just say select
all record within the last week but not prior to Sunday? So if it is
Wednesday today I would get the records for Monday, Tuesday, and
Wednesday.

I know I can calculate the values with a bunch of code-fu but I’d like
to avoid it if I can.

Time.now.monday <<-- midnight on monday morning

just do a Time.now.methods.sort.

there are a whole load of useful methods.

Hey Jason, this link may be of assistance to you.
http://www.rubyinside.com/advent2006/17-extendingar.html

Good luck,

-Conrad

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs