@groups = GroupsInfo.find(:all, :conditions=>[“date >= ? and date < ?”, @from_date.to_date ,@to_date.to_date])
I also should have mentioned that @to_date should be set to the day
after your ending target date:
@to_date.to_date + 1.day
One final note/suggestion…
It is also possible to use the hash form with a range.
Example:
from_date = Time.now.to_date - 10.days # 10 days ago
to_date = Time.now.to_date + 1.day # midnight one day after target @posts = Post.find(:all, :conditions => { :published_at =>
from_date…to_date })
SELECT * FROM “posts” WHERE (“posts”.“published_at” >= ‘2009-11-07’ AND
“posts”.“published_at” < ‘2009-11-18’)
Notice use of exclusive range (x…y), which would include all of
2009-11-17 up to but not including midnight of 2009-11-18.
I only mention all this because I tend to avoid database specific
implementation when possible/practical. However, there are definitely
practical uses for database specific implementations.
I don’t know if there are any performance considerations to be taken
into account in this particular case. I suppose one would be weighing
database CAST+date range test performance against datetime range
testing. My guess would be that the later would perform slightly better,
but it’s difficult to know without benchmarking.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.