How Do I Search Between a Date Rang Using the ActiveRecord Model?

Posted by Russ Bradberry on Stack Overflow See other posts from Stack Overflow or by Russ Bradberry
Published on 2010-05-07T17:36:23Z Indexed on 2010/05/07 17:38 UTC
Read the original article Hit count: 250

Filed under:
|
|
|
|

I am new to both Ruby and ActiveRecord. I currently have a need to modify and existing piece of code to add a date range in the select. The current piece goes like this:

ReportsThirdparty.find(:all, :conditions => {:site_id=>site_id, :campaign_id=>campaign_id, :size_id=>size_id})

Now, I need to add a range, but I am not sure how to do the BETWEEN or >= or <= operators. I guess what I need is something similar to:

ReportsThirdparty.find(:all, :conditions => {:site_id=>site_id, :campaign_id=>campaign_id, :size_id=>size_id, :row_date=>"BETWEEN #{start_date} AND #{end_date}")

Even if this did work, I know that using interpolation here would leave me subject to SQL injection attacks.

© Stack Overflow or respective owner

Related posts about activerecord

Related posts about ruby