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

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-

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:

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-

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 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-