# Adding automatic column calculation

**URL:** <https://www.ruby-forum.com/t/adding-automatic-column-calculation/140009>\
**Category:** Rails\
**Created:** [June 10, 2008, 2:55am UTC](https://www.ruby-forum.com/t/adding-automatic-column-calculation/140009 "2008-06-10T02:55:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![helzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e19b73/32.png) [@helzer](https://www.ruby-forum.com/u/helzer)\
**Post date:** [June 10, 2008, 2:55am UTC](https://www.ruby-forum.com/t/adding-automatic-column-calculation/140009/1 "2008-06-10T02:55:19Z")

</div>

Hi,

I want to add a column that would get calculated automatically  
whenever I update an object.  
For example, a checksum, of other columns in the model.

It should run whenever I do save, save!, update\_attributes, etc.

Where would be a logical place to add this in my model?

Thanks,  
Helzer

---

<div class="post-metadata">

**Author:** ![helzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e19b73/32.png) [@helzer](https://www.ruby-forum.com/u/helzer)\
**Post date:** [June 10, 2008, 3:38am UTC](https://www.ruby-forum.com/t/adding-automatic-column-calculation/140009/2 "2008-06-10T03:38:37Z")

</div>

On Jun 10, 1:54 am, helzer [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> I want to add a column that would get calculated automatically  
> whenever I update an object.  
> For example, a checksum, of other columns in the model.
> 
> It should run whenever I do save, save!, update\_attributes, etc.
> 
> Where would be a logical place to add this in my model?

before\_save ?

Fred

---

<div class="post-metadata">

**Author:** ![helzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e19b73/32.png) [@helzer](https://www.ruby-forum.com/u/helzer)\
**Post date:** [June 10, 2008, 4:24am UTC](https://www.ruby-forum.com/t/adding-automatic-column-calculation/140009/3 "2008-06-10T04:24:48Z")

</div>

class YourModel  
def before\_save  
your callback code …  
end  
end

[http://railsmanual.com/module/ActiveRecord::Callbacks](http://railsmanual.com/module/ActiveRecord::Callbacks)

---

<div class="post-metadata">

**Author:** ![helzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e19b73/32.png) [@helzer](https://www.ruby-forum.com/u/helzer)\
**Post date:** [June 10, 2008, 4:59am UTC](https://www.ruby-forum.com/t/adding-automatic-column-calculation/140009/4 "2008-06-10T04:59:53Z")

</div>

Thanks a lot Fred and alberto. I was just going to reinvent the wheel  
here…

Helzer
