# How to understand callback and validate\_associated

**URL:** <https://www.ruby-forum.com/t/how-to-understand-callback-and-validate-associated/192462>\
**Category:** Rails\
**Created:** [July 30, 2010, 6:01pm UTC](https://www.ruby-forum.com/t/how-to-understand-callback-and-validate-associated/192462 "2010-07-30T18:01:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [July 30, 2010, 6:01pm UTC](https://www.ruby-forum.com/t/how-to-understand-callback-and-validate-associated/192462/1 "2010-07-30T18:01:28Z")

</div>

1 after\_update callback is wrapped in transaction means:no matter  
exception occurs during saving an existing record, or in the  
after\_update method body,both the existing record and record  
manipulation in the after\_update body are all rollbacked?

2 class Book \< ActiveRecord::Base  
has\_many :pages  
validates\_associated :pages  
validates\_presence\_of :name  
end

validate\_associated means: when i invoke book.save to save book and it’s  
associations,no matter invalidation occurs during saving book self or  
saving any association,both book and all the associations don’t be  
saved?

I don’t understand these from document.can you help me to make it sure?  
Thanks!

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [July 31, 2010, 3:41am UTC](https://www.ruby-forum.com/t/how-to-understand-callback-and-validate-associated/192462/2 "2010-07-31T03:41:27Z")

</div>

anyone help me?

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [July 31, 2010, 9:00am UTC](https://www.ruby-forum.com/t/how-to-understand-callback-and-validate-associated/192462/3 "2010-07-31T09:00:28Z")

</div>

hi  
who know about it? thanks!

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [July 31, 2010, 11:48am UTC](https://www.ruby-forum.com/t/how-to-understand-callback-and-validate-associated/192462/4 "2010-07-31T11:48:04Z")

</div>

On Jul 31, 8:00 am, Guo Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> hi  
> who know about it? thanks!

Maybe you could rephrase your question - I for one couldn’t understand  
what you were asking

Fred

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [July 31, 2010, 4:43pm UTC](https://www.ruby-forum.com/t/how-to-understand-callback-and-validate-associated/192462/5 "2010-07-31T16:43:47Z")

</div>

I have described the problem clearly.Can you help me?

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [July 31, 2010, 12:49pm UTC](https://www.ruby-forum.com/t/how-to-understand-callback-and-validate-associated/192462/6 "2010-07-31T12:49:20Z")

</div>

thank!  
I copy the after\_update() doc as following:  
#######  
after\_update()

Is called after Base.save on existing objects that have a record. Note  
that this callback is still wrapped in the transaction around save. For  
example, if you invoke an external indexer at this point it wonâ€™t see  
the changes in the database.  
########  
I don’t understand “Note that this callback is still wrapped in the  
transaction around save”.  
if exception occurs when saving the existing record, or when executing  
after\_update method body,both the existing record’s saving and sql  
statements in the after\_update method body are all rollbacked?

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 1, 2010, 12:31am UTC](https://www.ruby-forum.com/t/how-to-understand-callback-and-validate-associated/192462/7 "2010-08-01T00:31:54Z")

</div>

On Jul 31, 11:49 am, Guo Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ########  
> I don’t understand “Note that this callback is still wrapped in the  
> transaction around save”.  
> if exception occurs when saving the existing record, or when executing  
> after\_update method body,both the existing record’s saving and sql  
> statements in the after\_update method body are all rollbacked?

Correct. The entirety of the save process (validations, before\_save,  
after\_save etc.) is wrapped in a single transaction.

Fred

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 1, 2010, 4:07am UTC](https://www.ruby-forum.com/t/how-to-understand-callback-and-validate-associated/192462/8 "2010-08-01T04:07:19Z")

</div>

Thanks,fred.
