[Rails] ActiveRecord and SQL SUM
Joseph Martin
jmartin at desertflood.com
Mon Jan 31 16:05:19 GMT 2005
I am a new Ruby / Rails user and I'm writing a small website to teach myself
both the language and the framework. I want to create a simple web interface to
the guest list for my upcoming wedding. I have two SQL tables: People and
Guests. The People table just has the usual name, address, telephone, etc. The
Guests table has a foreign key that links to People, a 'confirmed' field and a
'count' field. I use the count field to store the number of additional guests
that will be coming to the wedding in addition to the named person.
I want to be able to use the following query to find out how many people are
currently coming to the wedding: "SELECT SUM(count) FROM guests WHERE
guests.confirmed = 'y'". How can I best express that query in Rails with
ActiveRecord? I know I can use a "data piggyback" in a model class, but that
does not seem appropriate. The model class represents one row in the table. I
think this query should go into some kind of object that represents the table
(or GuestList) as a whole. But I'm unsure of where to put it and how to put it.
Can anyone help me with this?
Thanks,
~joe
More information about the Rails
mailing list