# How to Validate Child Object Virtual Attribute against Parent Object Field?

**URL:** <https://www.ruby-forum.com/t/how-to-validate-child-object-virtual-attribute-against-parent-object-field/154368>\
**Category:** Rails\
**Created:** [November 28, 2008, 10:35am UTC](https://www.ruby-forum.com/t/how-to-validate-child-object-virtual-attribute-against-parent-object-field/154368 "2008-11-28T10:35:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![djfobbz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dec6dc/32.png) [@djfobbz](https://www.ruby-forum.com/u/djfobbz)\
**Post date:** [November 28, 2008, 10:35am UTC](https://www.ruby-forum.com/t/how-to-validate-child-object-virtual-attribute-against-parent-object-field/154368/1 "2008-11-28T10:35:06Z")

</div>

Ok, I have the following:

Account =\> has\_many =\> Users  
Users =\> belong\_to =\> Account

When an Account is created, an ‘account\_code’ is also generated and  
stored within the Accounts table. Users must use this ‘account\_code’  
in order to Signup for the service. My problem is how do I compare  
the User submitted ‘account\_code’ in the Signup form against the  
‘account\_code’ stored in the parent object Account? Do I define this  
‘account\_code’ as a virtual attribute to the User model? Also, once I  
make the comparison, in case there is a mismatch, how do I add the  
error along with all other errors relating to User model?
