Virtual Columns and sort

274 views
Skip to first unread message

Ash

unread,
Oct 14, 2007, 8:07:09 AM10/14/07
to ActiveScaffold : Ruby on Rails plugin
Hi Folks,

I am pretty new to Ruby/Rails adn Active Scaffold so please forgive me
for anything very obvious that i've done wrong.

I have 2 models a tenant model and a bill model, the tenant can have a
number of bills. I have made a virtual column (total_bill) using the
helper convention: -


module AdminHelper
def total_bill_column(record)
record.bills.sum(:amount).to_s
end
end

I have also added the column to the scaffold config as follows: -

class AdminController < ApplicationController
active_scaffold :tenant do |config|
config.columns << :total_bill
config.columns[:total_bill].sort = "true"
end
end

According to: - http://activescaffold.com/docs/api-column the .sort
method should work, but it doesn't seem to. There is no link to click
on in the title bar

I tried using sort_by :method and I couldn't get either of those
working properly either (it complained about comparing floats and
string)

Anyone know what i'm doing wrong?
Thanks for your help
Ash

JeffS

unread,
Oct 16, 2007, 10:19:26 PM10/16/07
to ActiveScaffold : Ruby on Rails plugin
I am not totally sure how to go about this, but let me give it a shot.
This probably isn't totally correct since I really am a newbie at
this. However, it might give you a starting point.

First of all, I would suggest adding a column to your Bills model.
Call it "total_bills". Update it every time a bill is taken care of.
In your Bills controller:

class BillsController < ApplicationController
def before_update_save(record)
@total = (put code here to add up all bills)
record.total_bills = @total
end
end

Now you want to access total_bills in your Tenant model:

class Tenant < ActiveRecord::Base
has_many :bills
def total_bills
self.bills.total_bills if self.bills
end
end

In your Tenant controller:

active_scaffold do |config|
config.columns << :total_bills
config.columns[:total_bills].sort_by :sql => 'bills.total_bills'
end

That should add a virtual column called total_bills and allow you to
sort it.

Let me know if it actually works :)

Jeff


On Oct 14, 8:07 am, Ash <ash.mcconn...@gmail.com> wrote:
> Hi Folks,
>
> I am pretty new to Ruby/Rails adn Active Scaffold so please forgive me
> for anything very obvious that i've done wrong.
>
> I have 2 models a tenant model and a bill model, the tenant can have a
> number of bills. I have made a virtual column (total_bill) using the
> helper convention: -
>
> module AdminHelper
> def total_bill_column(record)
> record.bills.sum(:amount).to_s
> end
> end
>
> I have also added the column to the scaffold config as follows: -
>
> class AdminController < ApplicationController
> active_scaffold :tenant do |config|
> config.columns << :total_bill
> config.columns[:total_bill].sort = "true"
> end
> end
>

> According to: -http://activescaffold.com/docs/api-columnthe .sort

Reply all
Reply to author
Forward
0 new messages