Forum: Ruby on Rails Populating a column automatically based on the sum of other

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
C43ccfa65613c5bdfe3434dbe3b29fe1?d=identicon&s=25 idmkid (Guest)
on 2007-04-11 00:51
(Received via mailing list)
I am new to Ruby on Rails and love what I see. I am also new to MySQL.

I am using a generic table with the ajax_scaffold. The table and
application are working great. However, I want to populate one column
for a new row based on the sum of other columns inputed by the user
when a new row is created.

The row represents a new user. Some of the columns for the user
represent test scores. One column is for a total score. How do I add
up the columns with scores and populate the total score column

6d5602ac22cc57505c0d3a24b3d63108?d=identicon&s=25 Tyler Prete (Guest)
on 2007-04-11 00:56
(Received via mailing list)
You might want to look into Active Scaffold
It's the successor to ajax scaffold, and it makes things like column
really easy to do.

D7c511ce5025d37b8c6bd9134e0f2bd9?d=identicon&s=25 Thorsten (Guest)
on 2007-04-11 10:20
(Received via mailing list)
Simply use a AR Callback:

class User < Active_Record::Base
before_create :sum_scores

def sum_scores
  self.total_score = self.score1 + self.score2
This topic is locked and can not be replied to.