# Populating a column automatically based on the sum of other

**URL:** <https://www.ruby-forum.com/t/populating-a-column-automatically-based-on-the-sum-of-other/91637>\
**Category:** Rails\
**Created:** [April 11, 2007, 12:51am UTC](https://www.ruby-forum.com/t/populating-a-column-automatically-based-on-the-sum-of-other/91637 "2007-04-11T00:51:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![idmkid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/71e660/32.png) [@idmkid](https://www.ruby-forum.com/u/idmkid)\
**Post date:** [April 11, 2007, 12:51am UTC](https://www.ruby-forum.com/t/populating-a-column-automatically-based-on-the-sum-of-other/91637/1 "2007-04-11T00:51:29Z")

</div>

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  
dynamically?

Thanks,  
Idmkid

---

<div class="post-metadata">

**Author:** ![idmkid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/71e660/32.png) [@idmkid](https://www.ruby-forum.com/u/idmkid)\
**Post date:** [April 11, 2007, 10:20am UTC](https://www.ruby-forum.com/t/populating-a-column-automatically-based-on-the-sum-of-other/91637/2 "2007-04-11T10:20:13Z")

</div>

Simply use a AR Callback:

class User \< Active\_Record::Base  
before\_create :sum\_scores

private  
def sum\_scores  
self.total\_score = self.score1 + self.score2  
end  
end

---

<div class="post-metadata">

**Author:** ![idmkid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/71e660/32.png) [@idmkid](https://www.ruby-forum.com/u/idmkid)\
**Post date:** [April 11, 2007, 12:56am UTC](https://www.ruby-forum.com/t/populating-a-column-automatically-based-on-the-sum-of-other/91637/3 "2007-04-11T00:56:29Z")

</div>

You might want to look into Active Scaffold  
([http://www.activescaffold.com](http://www.activescaffold.com)).  
It’s the successor to ajax scaffold, and it makes things like column  
totals  
really easy to do.

–Tyler
