# Update first table when data is created in the second table

**URL:** <https://www.ruby-forum.com/t/update-first-table-when-data-is-created-in-the-second-table/128371>\
**Category:** Rails\
**Created:** [February 7, 2008, 4:28am UTC](https://www.ruby-forum.com/t/update-first-table-when-data-is-created-in-the-second-table/128371 "2008-02-07T04:28:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![koli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/41988e/32.png) [@koli](https://www.ruby-forum.com/u/koli)\
**Post date:** [February 7, 2008, 4:28am UTC](https://www.ruby-forum.com/t/update-first-table-when-data-is-created-in-the-second-table/128371/1 "2008-02-07T04:28:25Z")

</div>

Hi,

I want to know how to update data in first table when i create data in  
second table.

For ex:

1)locations-Fields(id,name,reply\_data)-TABLE ONE

2)Events-Fields(id,name)-TABLE TWO

I want to know how to update the reply\_data column in the first table  
when  
data is added to the Events table.

---

<div class="post-metadata">

**Author:** ![koli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/41988e/32.png) [@koli](https://www.ruby-forum.com/u/koli)\
**Post date:** [February 7, 2008, 11:34am UTC](https://www.ruby-forum.com/t/update-first-table-when-data-is-created-in-the-second-table/128371/2 "2008-02-07T11:34:07Z")

</div>

this should work with callbacks like after\_create or after\_save:

> **[ActiveRecord::Callbacks](https://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html)**
>
> Active Record Callbacks Callbacks are hooks into the life cycle of an Active Record object that allow you to trigger logic before or after a change in the object state.

just define them for table one and do whatever necessary with table two

---

<div class="post-metadata">

**Author:** ![koli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/41988e/32.png) [@koli](https://www.ruby-forum.com/u/koli)\
**Post date:** [February 7, 2008, 3:37pm UTC](https://www.ruby-forum.com/t/update-first-table-when-data-is-created-in-the-second-table/128371/3 "2008-02-07T15:37:36Z")

</div>

Thorsten M. wrote:

> this should work with callbacks like after\_create or after\_save:
> 
> [ActiveRecord::Callbacks](http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html)
> 
> just define them for table one and do whatever necessary with table two

Can u show me an example as to how to do it…

---

<div class="post-metadata">

**Author:** ![koli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/41988e/32.png) [@koli](https://www.ruby-forum.com/u/koli)\
**Post date:** [February 7, 2008, 4:21pm UTC](https://www.ruby-forum.com/t/update-first-table-when-data-is-created-in-the-second-table/128371/4 "2008-02-07T16:21:30Z")

</div>

in the model of table two:

after\_create :update\_reply\_data

def update\_reply\_data  
@record = TableOne.find(…)  
@record.update\_attribute(:reply\_data, new\_value)  
end

for:  
TableOne.find(…)  
must have some useful findcode, of course  
and:  
new\_value should be the new value for reply\_data,  
whatever that is, can’t see that in your question

if the tables are related in some form, things get  
more easy if you define this with belongs\_to,  
has\_many and so on. but again i can’t guess those  
details from your question

---

<div class="post-metadata">

**Author:** ![koli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/41988e/32.png) [@koli](https://www.ruby-forum.com/u/koli)\
**Post date:** [February 12, 2008, 11:29pm UTC](https://www.ruby-forum.com/t/update-first-table-when-data-is-created-in-the-second-table/128371/5 "2008-02-12T23:29:08Z")

</div>

On Feb 6, 2008 9:28 PM, Koli K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I want to know how to update the reply\_data column in the first table  
> when  
> data is added to the Events table.

Add an after\_save filter.

[http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html#M001298](http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html#M001298)

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)
