# Action when database changes

**URL:** <https://www.ruby-forum.com/t/action-when-database-changes/53811>\
**Category:** Rails\
**Created:** [February 14, 2006, 8:19pm UTC](https://www.ruby-forum.com/t/action-when-database-changes/53811 "2006-02-14T20:19:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jakub\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3be4f8/32.png) [@Jakub\_N](https://www.ruby-forum.com/u/Jakub_N)\
**Post date:** [February 14, 2006, 8:19pm UTC](https://www.ruby-forum.com/t/action-when-database-changes/53811/1 "2006-02-14T20:19:05Z")

</div>

Hello,

Is it possible to trigger an action in a controller when  
a table from database changes?  
I thought about using an utility like cron (railscron) to  
check every, e.g. 2 s, if the table changed, but I am wondering  
if there is a solution a bit more ‘elegant’: to trigger an action  
exactly when the change is made.

Best regards,

---

<div class="post-metadata">

**Author:** ![Jakub\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3be4f8/32.png) [@Jakub\_N](https://www.ruby-forum.com/u/Jakub_N)\
**Post date:** [February 14, 2006, 8:46pm UTC](https://www.ruby-forum.com/t/action-when-database-changes/53811/2 "2006-02-14T20:46:31Z")

</div>

On 2/14/06, Jakub N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Kuba Nowak
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

You could use the after\_save callback, along with the ruby http  
library, to ping the controller.

–  
Kyle M.  
Chief Technologist  
E Factor Media // FN Interactive  
removed\_email\_address@domain.invalid  
1-866-263-3261

---

<div class="post-metadata">

**Author:** ![Jakub\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3be4f8/32.png) [@Jakub\_N](https://www.ruby-forum.com/u/Jakub_N)\
**Post date:** [February 15, 2006, 11:24pm UTC](https://www.ruby-forum.com/t/action-when-database-changes/53811/3 "2006-02-15T23:24:07Z")

</div>

Kyle M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 2/14/06, Jakub N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Is it possible to trigger an action in a controller when  
> > a table from database changes?  
> > I thought about using an utility like cron (railscron) to  
> > check every, e.g. 2 s, if the table changed, but I am wondering  
> > if there is a solution a bit more ‘elegant’: to trigger an action  
> > exactly when the change is made.
> 
> You could use the after\_save callback, along with the ruby http  
> library, to ping the controller.

I have two independent application (Rails app and C++ app) which  
change the same table (but diffrent columns) in a database. The  
scenario is following: Rails app populates a table with some data to  
check and leaves the column ‘checked\_at’ null. Next C++ app checks  
this data and mark its activity by setting the column ‘checked\_at’ at  
the proper time. The results goes to new row in another table which  
“belongs” to Rails app, it means, it’s mapped to Rails class.

I don’t know if this idea is correct, but I would like to know how to  
find out when C++ app makes its work in order to take a glance (in  
Rails app) at the results.

Any help, any ideas will be (very :)) apprecieted.

---

<div class="post-metadata">

**Author:** ![Jakub\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3be4f8/32.png) [@Jakub\_N](https://www.ruby-forum.com/u/Jakub_N)\
**Post date:** [February 15, 2006, 11:39pm UTC](https://www.ruby-forum.com/t/action-when-database-changes/53811/4 "2006-02-15T23:39:19Z")

</div>

On 2/15/06, Jakub N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > library, to ping the controller.  
> > find out when C++ app makes its work in order to take a glance (in

C++ system command executes curl/short ruby script that pings the  
action?

–  
Kyle M.  
Chief Technologist  
E Factor Media // FN Interactive  
removed\_email\_address@domain.invalid  
1-866-263-3261

---

<div class="post-metadata">

**Author:** ![Jakub\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3be4f8/32.png) [@Jakub\_N](https://www.ruby-forum.com/u/Jakub_N)\
**Post date:** [February 16, 2006, 8:36pm UTC](https://www.ruby-forum.com/t/action-when-database-changes/53811/5 "2006-02-16T20:36:45Z")

</div>

Dave T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> However, after further thought, I’m wondering why you need all this  
> complexity? The only reason I can see why you’d need to have the Rails  
> app check the db every few seconds is because you are caching something.

Each user of my application can send a task (a request) for which the  
calculating time may vary from one up to several minutes. So it’s  
obvious that I’m forced to make him wait. But I don’t want to. I think  
that if I used a rails app to submit tasks and another app to give the  
response I would need a trick to notify a user that his task was  
calculated. That’s the reason I wanted to check the state of database  
to find out which tasks of which users are finished.

Maybe should I use a queuing system or something like that?

Performance is the issue. I will have about 2000 users, and wait for  
demands of about 100 users at once.

Best regards,

---

<div class="post-metadata">

**Author:** ![Jakub\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3be4f8/32.png) [@Jakub\_N](https://www.ruby-forum.com/u/Jakub_N)\
**Post date:** [February 16, 2006, 9:22pm UTC](https://www.ruby-forum.com/t/action-when-database-changes/53811/6 "2006-02-16T21:22:03Z")

</div>

On 2/16/06, Jakub N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> calculated. That’s the reason I wanted to check the state of database
> 
> > upon every request and recompute your values. Only in slow Java
> > 
> > > > > > if there is a solution a bit more ‘elegant’: to trigger an action  
> > > > > > the proper time. The results goes to new row in another table which
> > > > 
> > > > * * *
> > > 
> > > E Factor Media // FN Interactive  
> > > Cheers!  
> > > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)  
> > > –  
> > > Kuba Nowak
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Look at this article for an example of running a queue with RailsCron:  
[http://kylemaxwell.com/articles/2006/01/26/simple-emailqueue-for-rails](http://kylemaxwell.com/articles/2006/01/26/simple-emailqueue-for-rails)

For something like this, I prefer to send the user an email informing  
him/her that the task is complete. O’Reilly Safari also sends emails  
in similar situations.

–  
Kyle M.  
Chief Technologist  
E Factor Media // FN Interactive  
removed\_email\_address@domain.invalid  
1-866-263-3261

---

<div class="post-metadata">

**Author:** ![Jakub\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3be4f8/32.png) [@Jakub\_N](https://www.ruby-forum.com/u/Jakub_N)\
**Post date:** [February 15, 2006, 11:54pm UTC](https://www.ruby-forum.com/t/action-when-database-changes/53811/7 "2006-02-15T23:54:31Z")

</div>

Kyle beat me to the curl suggestion 🙂

However, after further thought, I’m wondering why you need all this  
complexity? The only reason I can see why you’d need to have the Rails  
app check the db every few seconds is because you are caching something.  
I would simply have the Rails app grab the data directly from the DB  
upon every request and recompute your values. Only in slow Java  
applications have I found caching very useful…

On Wednesday, February 15, 2006, at 2:37 PM, Kyle M. wrote:

> > > You could use the after\_save callback, along with the ruby http  
> > > I don’t know if this idea is correct, but I would like to know how to  
> > > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Cheers!  
–Dave T.  
[http://devlists.com](http://devlists.com) - Email list management  
[http://palmsphere.com](http://palmsphere.com) - Apps for your hand-held

---

<div class="post-metadata">

**Author:** ![Jakub\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3be4f8/32.png) [@Jakub\_N](https://www.ruby-forum.com/u/Jakub_N)\
**Post date:** [February 16, 2006, 9:50pm UTC](https://www.ruby-forum.com/t/action-when-database-changes/53811/8 "2006-02-16T21:50:07Z")

</div>

The approach I would take is to have the the task processor have a  
“completion” flag/timestamp in the database, and corresponding  
“has\_been\_read” flag/timestamp, or something along those lines. Any time  
the user requests a page in the app, check to see if there are any  
completed, unread notices and use the :flash to notify them somewhere in  
the layout. Think along the lines of a “New Mail” alert on a portal or  
webmail service.

If you want something more proactively pushed out, maybe because the app  
window gets left open idle for long periods after task submission, maybe  
have a div in your layout reserved for that sort of notice and have a  
periodically\_call\_remote() check for newly completed jobs and put an  
elert in the div. Use it sparingly, though, to save the server from  
thousands of status checks a minute. Maybe only bring it into play in a  
view when the user has a pending submitted task.

-sk

Jakub N. wrote:

> Dave T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > However, after further thought, I’m wondering why you need all this  
> > complexity? The only reason I can see why you’d need to have the Rails  
> > app check the db every few seconds is because you are caching something.
> 
> Each user of my application can send a task (a request) for which the  
> calculating time may vary from one up to several minutes. So it’s  
> obvious that I’m forced to make him wait. But I don’t want to. I think  
> that if I used a rails app to submit tasks and another app to give the  
> response I would need a trick to notify a user that his task was  
> calculated. That’s the reason I wanted to check the state of database  
> to find out which tasks of which users are finished.
> 
> Maybe should I use a queuing system or something like that?
> 
> Performance is the issue. I will have about 2000 users, and wait for  
> demands of about 100 users at once.
> 
> Best regards,
