Generating a report by date range in rails

How would you go about producing reports by user selected date ranges in a rails app? What are the best date range pickers?

edit in response to patrick : I am looking for a bit of both widget and active record advice but what I am really curious about is how to restfully display a date ranged list based on user selected dates.

-------------Problems Reply------------

Are we asking an interface question here (i.e. you want a widget) or an ActiveRecord question?

Date Picking Widgets

1) Default Rails Solution: See date_select documentation here.

2) Use a plugin : Why write code? I personally like the CalendarDateSelect plugin, using a pair of the suckers when I need a range.

3) Adapt a Javascript widget to Rails: It is almost trivial to integrate something like the Yahoo UI library (YUI) Calendar, which is all Javascript, to Rails. From the perspective of Rails its just another way to populate the params[:start_date] and params[:end_date]. YUI Calendar has native support for ranges.

Getting the data from the Widgets

1) Default Rails Solution See date_select documentation here.

#an application helper method you'll find helpful
#credit to http://blog.zerosum.org/2007/5/9/deconstructing-date_select

# Reconstruct a date object from date_select helper form params
def build_date_from_params(field_name, params)
Date.new(params["#{field_name.to_s}(1i)"].to_i,
params["#{field_name.to_s}(2i)"].to_i,
params["#{field_name.to_s}(3i)"].to_i)
end

#goes into view
<%= date_select "report", "start_date", ... %>
<%= date_select "report", "end_date", ... %>

#goes into controller -- add your own error handling/defaults, please!
report_start_date = build_date_from_params("start_date", params[:report])
report_end_date = build_date_from_params("end_date", params[:report])

2) CalendarDateSelect: Rather similar to the above, just with sexier visible UI.

3) Adapt a Javascript widget: Typically this means that some form element will have the date input as a string. Great news for you, since Date.parse is some serious magic. The params[:some_form_element_name] will be initialized by Rails for you.

#goes in controller. Please handle errors yourself -- Javascript != trusted input.
report_start_date = Date.parse(params[:report_start_date])

Writing the call to ActiveRecord

Easy as pie.

#initialize start_date and end_date up here, by pulling from params probably
@models = SomeModel.find(:all, :conditions => ['date >= ? and date <= ?',
start_date, end_date])
#do something with models

It's not an unRESTful practice to have URL parameters control the range of selected records. In your index action, you can do what Patrick suggested and have this:

#initialize start_date and end_date up here, by pulling from params probably
@models = SomeModel.find(:all, :conditions => ['date >= ? and date <= ?', params[:start_date], params[:end_date]])

Then in your index view, create a form that tacks on ?start_date=2008-01-01&end_date=2008-12-31 to the URL. Remember that it's user-supplied input, so be careful with it. If you put it back on the screen in your index action, be sure to do it like this:

Showing records starting on
<%= h start_date %>
and ending on
<%= h end_date %>

Category:ruby on rails Views:0 Time:2008-09-26

Related post

  • How to Properly Convert or Query Date Range for Rails / MySQL DateTime Column 2009-03-11

    I have a rails application where I store created_at as datetime (standard). I am building a form for searching and I find I have to use find_by_sql to do some complex subqueries. The form has a date range (no time) to search on for items created_at f

  • Best way to return array of counted values for multiple date ranges in Rails 2009-12-11

    I'm trying to return an array of counted values for consecutive date ranges (that will vary in distance), so that I have something like the following: counted_values = [2,0,4,5,6,0,0,4,2] So far I've written something that works but makes me shudder

  • Year Comparison Report by Date Range 2012-02-14

    I have a query set up for a report that uses a date range to filter the data. I am needing to have that date range be automatically compared to the previous year in the same date range. I am having issues with it being able to pull the data for the p

  • Access 2003 report by date range 2014-12-27

    I want to print reports for a date range and want to be prompted for beginning and ending dates. Do I do this in a query or what? I cant for the life of me rember how to do this --------------Solutions------------- You can do it in a query just by us

  • How can I search date range in Rails with variable date 2011-03-12

    How can I do this in rails active record¿? find all models that match (created_at + 100 days between this month) Edit: Ok, Sorry for not be precise this what I'm trying to do in active record in Rails 3.0 way: select distinct p.ID from patients p inn

  • Help with generating a report from data in a parent-children model 2010-03-01

    I need help with a problem regarding data saved in a parent-children model table and a report I need to build upon it. I've already tried searching for topics about parent-children issues, but I couldn't find anything useful in my scenario. What I ha

  • generate days from date range 2010-01-28

    I would like to run a query like select ... as days where `date` is between '2010-01-20' and '2010-01-24' And return data like: days ---------- 2010-01-20 2010-01-21 2010-01-22 2010-01-23 2010-01-24 --------------Solutions------------- This solution

  • Rails - Trying to query from a date range-everything from today 2010-04-26

    This question already has an answer here: Rails ActiveRecord date between 8 answers I'm trying to figure the best way to query a date range from rails...I looked around on Google but am unsure about how to use this syntax. I have a Model that has var

  • Report Date Range Issues 2013-10-06

    I am using project professional 2013 to track manpower for numerous concurrent projects which are listed in one project file as individual tasks. For each "task" I have allocated up to four trades as resources. I have altered my resource allotment ma

  • How to make a date range display on an Access 2003 report? 2014-07-31

    Hi all. I have some reports by date range I'm working with and I would like the dates I input to automatically display at the head of the report. I suspect there is a simple solution to this, but I just can't figure it out. I appreciate any help! Tha

  • Grouping totals by date ranges 2010-10-07

    I have to get totals from a table using different criteria, which I do like this: SELECT DISTINCT SUM(CASE WHEN MYCONDITION1 THEN 1 ELSE 0 END) AS TOTAL1, SUM(CASE WHEN MYCONDITION2 THEN 1 ELSE 0 END) AS TOTAL2 FROM TABLE1, TABLE2 WHERE COMMON_CONDIT

  • sumif criteria date range 2013-04-21

    About once a year I have a formula for which the exact expression escapes me and have really appreciated the help I've received from the Forums. I was hoping I could prevail upon you all again. Here's the problem: First of all, I'm constrained by a s

  • How can I generate convenient date ranges based on a given NSDate? 2009-08-25

    I'm creating a report generator in Cocoa, and I need to produce convenient date ranges such as "Today", "This Week", "This Month", "This Year", etc. Is there a good way to do this? Here is my skeleton thus far: @interface DateRange : NSObject { NSDat

  • how to build a select report form with date range (Microsoft Access 2007) 2012-09-04

    Good day to everyone. My name is Maria. I was trying to generate a report using a Select report Form with date range using outbound texts (from/txtStartDate) and (to/txtEndDate) to limit it. I have established the report and the Select report form bu

  • Report communicating with Query date range 2012-12-28

    I am having problems with my report only generating the date range that i am putting in specific on my query. It is pulling all the incidents that i have in the query and i only want the date range of 2/1/2013 thru 2/28/2013. I solved my problem by g

  • Summary report grouped on multiple date ranges 2009-09-25

    I need to create a sales & commission report Basically it goes (please forgive the blatent craziness of the SaleDate table, but I'm simplifying the business logic, and in reality it actually makes sense to have it this way) SELECT agentName, SUM(

  • Code to generate Crystal Reports report works fine in CLI, but "Data source name not found" when accessed from browser 2011-05-09

    I'm trying to generate a report from PHP, using Crystal Reports, The code seems to be correct: <?php set_time_limit(0); if(isset($_GET['id'])) { $id = $_GET['id']; } else { die('Please specify an ID'); } $path = "c:\\wamp\\www\\billing\\reports";

  • rails group_by relative date ranges 2011-07-12

    I have a client who wants to see a report of her employees group by the date they took their last position (date_in_position). She wants them grouped by Less than 1 year, 1-3 years, 3-5 years, and over 5 years. I made a little method to return a stri

  • Strange Behavior with mongoid date range query. - Rails 3 2011-07-22

    I am running in very strange date range query problem. When I use DateTime.now.utc option to query database, the query date for DateTime.now is different that my current DateTime.now(i.e. one that's returned on console.) My mogoid.yml uses- use_utc:

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.094 (s). 11 q(s)