# Need Help on How to provide a Start and Stop Date range Dynamically to my ActiveRecord Find?

**URL:** <https://www.ruby-forum.com/t/need-help-on-how-to-provide-a-start-and-stop-date-range-dynamically-to-my-activerecord-find/140948>\
**Category:** Rails\
**Created:** [June 20, 2008, 12:55am UTC](https://www.ruby-forum.com/t/need-help-on-how-to-provide-a-start-and-stop-date-range-dynamically-to-my-activerecord-find/140948 "2008-06-20T00:55:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![scottc12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71c47a/32.png) [@scottc12](https://www.ruby-forum.com/u/scottc12)\
**Post date:** [June 20, 2008, 12:55am UTC](https://www.ruby-forum.com/t/need-help-on-how-to-provide-a-start-and-stop-date-range-dynamically-to-my-activerecord-find/140948/1 "2008-06-20T00:55:44Z")

</div>

I’d like to have my index page present the user with a Form for  
selecting a Date range

Example:  
Start 2008-06-19 End: 2008-06-29 and then pass this information into  
my ActiveRecord Find

I’m working with a legacy DB and with the help of the list Im able to  
do all my queries and understand what’s going on but I’m having  
trouble figuring out the method to pass my gathered Date Range search  
to my Find.

I’m assuming I would want to use form tag and present this from the  
index page and results to the index page as well?

My Current Static Find:  
@reports = Report.find(:all, :conditions =\> “expiration\_date between  
‘2008-06-19’ and ‘2008-06-29’”, :order =\> ‘expiration\_date DESC’)

Still just a newb but the power of ActiveRecord is fantastic.

thanks

Sc-

---

<div class="post-metadata">

**Author:** ![scottc12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71c47a/32.png) [@scottc12](https://www.ruby-forum.com/u/scottc12)\
**Post date:** [June 20, 2008, 5:41pm UTC](https://www.ruby-forum.com/t/need-help-on-how-to-provide-a-start-and-stop-date-range-dynamically-to-my-activerecord-find/140948/2 "2008-06-20T17:41:49Z")

</div>

On your homepage build a form with two date\_select fields in it, one  
called start\_date and one called end\_date. Then, in your controller  
action, you do

@reports = Report.find(:all, :conditions =\> {:expiration\_date =\>  
(params[:start\_date]…params[:end\_date])})

ActiveRecord will automatically build the date range in the select  
statement based on your particular database adapter.

scottc12 wrote:

> I’d like to have my index page present the user with a Form for  
> selecting a Date range
> 
> Example:  
> Start 2008-06-19 End: 2008-06-29 and then pass this information into  
> my ActiveRecord Find
> 
> I’m working with a legacy DB and with the help of the list Im able to  
> do all my queries and understand what’s going on but I’m having  
> trouble figuring out the method to pass my gathered Date Range search  
> to my Find.
> 
> I’m assuming I would want to use form tag and present this from the  
> index page and results to the index page as well?
> 
> My Current Static Find:  
> @reports = Report.find(:all, :conditions =\> “expiration\_date between  
> ‘2008-06-19’ and ‘2008-06-29’”, :order =\> ‘expiration\_date DESC’)
> 
> Still just a newb but the power of ActiveRecord is fantastic.
> 
> thanks
> 
> Sc-

---

<div class="post-metadata">

**Author:** ![scottc12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71c47a/32.png) [@scottc12](https://www.ruby-forum.com/u/scottc12)\
**Post date:** [June 21, 2008, 1:26pm UTC](https://www.ruby-forum.com/t/need-help-on-how-to-provide-a-start-and-stop-date-range-dynamically-to-my-activerecord-find/140948/3 "2008-06-21T13:26:32Z")

</div>

Thanks Brent,  
I was thinking I needed to build things in reports/index and then  
have them display in show which from my reading is for single returned  
ID. Your saying I can just add this to my homepage and then render. I  
assume I could also make this a single page with inline results using  
AJAX?

Thanks–  
Sc-

On Jun 20, 11:41 am, Brent M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![scottc12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71c47a/32.png) [@scottc12](https://www.ruby-forum.com/u/scottc12)\
**Post date:** [June 22, 2008, 7:48am UTC](https://www.ruby-forum.com/t/need-help-on-how-to-provide-a-start-and-stop-date-range-dynamically-to-my-activerecord-find/140948/4 "2008-06-22T07:48:09Z")

</div>

From a usability point of view I’d put the search form on the index page  
and all other pages in the controller. That’s up to you and your app  
design though. You’re right that the show action should only be used  
for displaying single records, if your app is sticking to the RESTful  
conventions. That’s not always appropriate, though, so it’s up to you.

I tend to overload my index action to handle filtering: the view code  
for a search result is 99.9% the same as for a straight index – the  
only difference is the line that generates the collection for display  
(and visual confirmation for the user that they’re looking at a subset).  
For maximum DRYness I just check at the top of my index action if I’m  
running a search or not:

def index  
if params[:search\_param]  
conditions = whatever  
end  
@reports = Report.find(:all, :conditions =\> conditions)  
end

Doing the same thing w/ ajax is near trivial: you just need an RJS  
template to render the response.

Brent

scottc12 wrote:

> Thanks Brent,  
> I was thinking I needed to build things in reports/index and then  
> have them display in show which from my reading is for single returned  
> ID. Your saying I can just add this to my homepage and then render. I  
> assume I could also make this a single page with inline results using  
> AJAX?
> 
> Thanks–  
> Sc-
> 
> On Jun 20, 11:41ï¿½am, Brent M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![scottc12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71c47a/32.png) [@scottc12](https://www.ruby-forum.com/u/scottc12)\
**Post date:** [June 23, 2008, 3:05pm UTC](https://www.ruby-forum.com/t/need-help-on-how-to-provide-a-start-and-stop-date-range-dynamically-to-my-activerecord-find/140948/5 "2008-06-23T15:05:49Z")

</div>

Thanks so much. I took your advice and created a new controller and  
have things successfully posting params. I should have started with a  
ROR complaint DB instead of working with a Legacy DB for my first  
“real” project. In any case I’m progressing and although its messy at  
the moment I’m functional. I’m going to spend the day soaking up  
ActiveRecord and cleaning up controllers and models.

Thanks again.  
Sc-

On Jun 22, 1:48Â am, Brent M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
