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.
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.
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 Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com
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.