Threadeds Ruby on Rails Cheat Sheet

Some little snippets of code to act as crib notes for Ruby on Rails.

		
def average_time_per_document_per_job
                if nil != params[:start_date]
                  @old_start_date = Date.new( params[:start_date][:year].to_i, params[:start_date][:month].to_i, params[:start_date][:day].to_i)
                        start_date = params[:start_date][:year] + "-" + params[:start_date][:month] + "-" + params[:start_date][:day]
                else
                        @old_start_date = Date.today
                        start_date = @old_start_date.year.to_s + "-" + @old_start_date.month.to_s + "-" + @old_start_date.mday.to_s
                end

                if nil != params[:end_date]
            @old_end_date = Date.new( params[:end_date][:year].to_i, params[:end_date][:month].to_i, params[:end_date][:day].to_i)
                  end_date = params[:end_date][:year] + "-" + params[:end_date][:month] + "-" + params[:end_date][:day]
                else
            @old_end_date = Date.today
                        end_date = @old_end_date.year.to_s + "-" + @old_end_date.month.to_s + "-" + @old_end_date.mday.to_s
                end

    k =0
    @job=[]
    @result=[]
    jobs = Job.find(:all)
    jobs.each do |jobby|
      totalTime = 0
      totalPages = 0
      rawStits = RawStat.find( :all, :conditions => "job = '" + jobby.name + "' and tstamp >= '" + start_date + "' and tstamp < '" + end_date + "'")
      rawStits.each do |stat|
        totalTime = totalTime + stat.totaltime
              totalPages = totalPages + stat.totalpages
      end
      @job[ k] = jobby.name
                        if 0 == totalPages
                                @result[ k] = 0
                        else
        @result[ k] = totalTime/totalPages
                        end
      k = k +1
    end
  end

	
 

prototype.js

this works yet this is better by 21k
						
<html>
<head>
...
<%= define_javascript_functions %>
...
</head>
<body>

					
						
<html>
<head>
...
<%= javascript_include_tag "prototype" %>
...
</head>
<body>

					
 

Workaround on discard_day => true bug in select_date

		
<%=
select_year( @old_start_date, :prefix => "start_date" )
%>
<%=
select_month( @old_start_date, :prefix => "start_date" )
%>

	
 

Workaround to bug selecting 'selected' in select

		
<% @jobs = Job.find(:all, :order => "name") %>
<select name="job[id]">
   <option value=""></option>
   <% @jobs.each do |job| %>
       <option value="<%= job.name %>"
         <%= ' selected' if job.name == @job_selected %>
         <%= job.name %>
       </option>
   <% end %>
</select>

	

Copyright threaded.com 2005. All rights reserved.