How to select rows between two dates?

Hi.

I have a table with two fields, from_date and to_date. Now I want to
do something like this:

SELECT * FROM table WHERE from_date < TODAY AND to_date > today;

I’m new to Ruby on Rails, so any help is much appreciated.

find(:all, :conditions => [“from_data < ? AND to_date > ?”, TODAY,
today])

I think you’d better read some tutorial books at first. Agile Web
Development With Rails can be a good start.

On Sep 6, 2007, at 10:18 AM, Kim G wrote:

Hi.

I have a table with two fields, from_date and to_date. Now I want to
do something like this:

SELECT * FROM table WHERE from_date < TODAY AND to_date > today;

I’m new to Ruby on Rails, so any help is much appreciated.

class Daterange < ActiveRecord::Base

to match your example, not that this should ever be a real name

set_table_name ‘table’

def self.as_of(date=Time.now)
self.find(:all,
:conditions => [ ‘? BETWEEN from_date AND to_date’,
date.to_s(:db) ])
end
end

Then just call:
Daterange.as_of(3.days.from_now)

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Thanks to both of you.

Rob’s solution was exactly what I was looking for. The SQL and table
name was only to illustrate the problem.

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