Group by in Rails View

In my rails application I have following models

  belongs_to :account
  belongs_to :agent
  belongs_to :program

And here is the query i used to get data

def self.efficiency_report(starts=nil, ends=nil)
sql = "SELECT p.abbreviation,,
        FROM transactions t
        inner join accounts a on t.account_id =
        inner join programs p on a.program_id =
        inner join agents ag on t.agent_id =
        Group by ," 

def self.result_array(sql)
  conn = ActiveRecord::Base.connection
  res = conn.execute(sql)
  results = []
    results << r
  return results

I want to render data in view with group by program first then group by agent name under it then miles, like this

    Miles          Date 
    1234           02/12/2012           
    5463           03/12/2012 
    Miles          Date 
    234           02/22/2012           
    344           01/02/2012 

    Miles          Date 
    1234           02/12/2012           
    5463           03/12/2012 
    Miles          Date 
    234           02/22/2012           
    344           01/02/2012

For this i m doing following in my view

%h2 Vendor Efficiency Report
- @transactions.group_by(&:row[0]).sort.each { |data, transaction|
%h2= data.row[0]
- transaction.group_by(&:row[1]).sort.each { |data, transaction|
%table#data_table.display{:cellpadding => "0", :cellspacing => "0"}
  %h3{:style => "clear:both;margin-left:10px"}= data.row[1]
      %td.bold{:style => "width:60px;"} Miles
      %td.bold{:style => "width:60px;"} Date
  - for t in transaction
       %td{:style => "width:60px;"}= row[2] 
       %td{:style => "width:60px;"}= row[3]
= will_paginate @transactions

But i m getting this error

wrong argument type String (expected Proc)

Will anyone let me know what i m doing wrong here or is there any other better way for grouping?

Thanks in advance


The problem is your group_by calls. I'm not sure what &:row[0] is doing, but it's passing a string as argument. I think this is what you're looking for:

@transactions.group_by{ |r| r[0] }...


Just figured out what &:row[0] was doing. It's a call to the Symbol#[] method, which returns the character at the index given (essentially, as if it were aString). The call :row[0] returns the first character of the string "row".

So basically, it was as if you were calling:


Need Your Help

From Elixir to SqlAlchemy Declarative, Polymorphic Nodes with Child Inheritance

python sqlalchemy python-elixir

I have already finished a good bit of my python/Elixir interface on my existing database. I am now considering to drop Elixir and move everything into pure SQLAlchemy, most likely wanting to use

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.