Friday, June 22, 2007

A better group_by "in_groups_by" for ActiveRecord

Background

Active record's find returns an array, you can use group_by to put the result into a groups. However group_by returns a hash, a hash does not have any order, When you iterate thru the hash it comes out in any order.

Better Approach

Write a function to return a array of records grouped by some critiera (Like in_groups_of). Now you can simply iterate thru using each and each. In my implementation I have assumed the data is already ordered

The Code

class Array
  def in_groups_by
    # Group elements into individual array's by the result of a block
    # Similar to the in_groups_of function.
    # NOTE: assumes array is already ordered/sorted by group !!
    curr=nil.class 
    result=[]
    each do |element|
       group=yield(element) # Get grouping value
       result << [] if curr != group # if not same, start a new array
       curr = group
       result[-1] << element
    end
    result
  end
end

Go on Give it a go, Copy and paste the below code into say the bottom of "environment.rb", re-cycle the server, and try

An Example

def customers_grouped_by_country
  ds=Customer.find(:all, :order => :order=>"county_id")
  ds.in_groups_by(&:county_id)
  # or the alternative syntax 
  # ds.in_groups_by { |r| r.country_id }
end

<% customers_grouped_by_country.each do |group| %>
  <h1>Country <%= group[0].country_id></h1>
  <% group.each do |e| %>
    <p><%= e.name %></p>
  <% end %>
<% end >

Thursday, June 21, 2007

Tuesday, June 19, 2007

Disable Browsers Autocomplete on a Rails Form

Intro

Autocomplete on a form can cause the observe_field not to fire (I was using on => keyup, i am going to change to on => blur and test)

Also the users get confused if too many things on a form, start autocompeting

The Solutuion

Simply disable autocomplete, Tested on IE/Firefox. This can be done at the form level

<% form_tag( {:action => 'post'}, { :autocomplete => :off } ) do %>

Running Rails Apps More Reliability under IIS

Intro

I have been attempting to get the new FastCGI for IIS preview to run on IIS5/6 as detailed previously.

Yes it runs just, But is unrelaible when you perform http posts. However the exersise had one good side effect.

The Good News

The blog at 10 steps to get Ruby on Rails running on Windows with IIS FastCGI detailed a change you need to make to the cgi.rb. because IIS does not support No Parsed Headers (NPH)

Once this change was in place, My web site using the older fastcgi runs much more reliably

# The changes is to lib\ruby\1.8\cgi.rb, 
# is to comment out the refereence to IIS
 
Line 559: if options.delete("nph") # or /IIS/n.match(env_table['SERVER_SOFTWARE'])

The Bad News

I would perferr to use the new FastCGI for IIS when it comes out, My problem issue is detailed on the IIS FastCGI Handler Forum hopefully some one will details a solution.

Friday, June 8, 2007

ADO Explorer A sql tool for Access/MS-SQL/VFP

I just spent the last day converting data from a legacy access application to My Rails app (back end "dictated" db ms-sql), It was a tedious exercuise using Access and MS's Managment Studio.

Years ago developed an application that could access any ADO database using a common interface. So I developed it again, but made it better. Conversion was a breeze. This tool affectionately called ADOExploer is available here ADO Explorer Page. Feedback so-far has been good, enjoy

Google