Group by in Rails View

In my rails application I have following models

Transaction
  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,ag.name,
         t.miles, t.date
        FROM transactions t
        inner join accounts a on t.account_id = a.id
        inner join programs p on a.program_id = p.id
        inner join agents ag on t.agent_id = ag.id
        Group by p.id , ag.id" 
 result_array(sql)
end


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

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

Program:AA
  Agent:Bob
    Miles          Date 
    1234           02/12/2012           
    5463           03/12/2012 
 Agent:Ben
    Miles          Date 
    234           02/22/2012           
    344           01/02/2012 

Program:BB
  Agent:Bob
    Miles          Date 
    1234           02/12/2012           
    5463           03/12/2012 
  Agent:Ben
    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]
  %thead
    %tr.odd
      %td.bold{:style => "width:60px;"} Miles
      %td.bold{:style => "width:60px;"} Date
  - for t in transaction
    %tbody
      %tr
       %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

Answers


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] }...

Edit

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:

@transactions.group_by(&"r")

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.