# Update all records in the db

**URL:** <https://www.ruby-forum.com/t/update-all-records-in-the-db/78419>\
**Category:** Rails\
**Created:** [December 5, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/update-all-records-in-the-db/78419 "2006-12-05T17:42:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [December 5, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/update-all-records-in-the-db/78419/1 "2006-12-05T17:42:08Z")

</div>

hi @ all

I have some information (a value and a percentage number) saved in the  
database. for example 5 records. I wrote a model, who’s look like as the  
following model:

date value percentagenumber  
2006-12-02 10 0.0  
2006-12-03 10.5 5.0  
2006-12-04 10.71 2.0  
2006-12-05 10.6029 -1.0

Every percentage-number are dependent on the value. When I change the  
percentage-number 5.0 to 6.0 of the record “2006-12-03”, so every number  
are changed who’s younger than the changed one (“2006-12-03”,  
“2006-12-04”, “2006-12-05”). How can I realize this in my model? Does  
anyone can help me? How can I iterate to my records?

an early reply will oblige

greets ma

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [December 5, 2006, 6:20pm UTC](https://www.ruby-forum.com/t/update-all-records-in-the-db/78419/2 "2006-12-05T18:20:05Z")

</div>

Hi !

2006/12/5, M. R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I have some information (a value and a percentage number) saved in the  
> database. for example 5 records. I wrote a model, who’s look like as the  
> following model:

I believe what you want is a callback. In your case, it looks like an  
after save callback will do what you want:

class Percentage \< AR::Base  
after\_save :update\_related\_models

protected  
def update\_related\_models  
# Do what you need with the other models here. This will only be  
called  
# after this model is saved.  
end  
end

For the full details,  
[http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html](http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html)

## Hope that helps !

FranÃ§ois Beausoleil  
[http://blog.teksol.info/](http://blog.teksol.info/)  
[http://piston.rubyforge.org/](http://piston.rubyforge.org/)

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [December 5, 2006, 7:32pm UTC](https://www.ruby-forum.com/t/update-all-records-in-the-db/78419/3 "2006-12-05T19:32:19Z")

</div>

2006/12/5, M. R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> And how can I use this callback for all records?

As soon as the record is saved, the callback will fire. You decide  
what to do in the callback.

## Bye !

FranÃ§ois Beausoleil  
[http://blog.teksol.info/](http://blog.teksol.info/)  
[http://piston.rubyforge.org/](http://piston.rubyforge.org/)

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [December 5, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/update-all-records-in-the-db/78419/4 "2006-12-05T19:08:21Z")

</div>

And how can I use this callback for all records?

FranÃ§ois Beausoleil wrote:

> Hi !
> 
> 2006/12/5, M. R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > I have some information (a value and a percentage number) saved in the  
> > database. for example 5 records. I wrote a model, who’s look like as the  
> > following model:
> 
> I believe what you want is a callback. In your case, it looks like an  
> after save callback will do what you want:
> 
> class Percentage \< AR::Base  
> after\_save :update\_related\_models
> 
> protected  
> def update\_related\_models  
> # Do what you need with the other models here. This will only be  
> called  
> # after this model is saved.  
> end  
> end
