# Validation and update

**URL:** <https://www.ruby-forum.com/t/validation-and-update/54690>\
**Category:** Rails\
**Created:** [March 13, 2006, 7:57am UTC](https://www.ruby-forum.com/t/validation-and-update/54690 "2006-03-13T07:57:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [March 13, 2006, 7:57am UTC](https://www.ruby-forum.com/t/validation-and-update/54690/1 "2006-03-13T07:57:20Z")

</div>

Hi,

I have validation working in a situation similar to the following.  
(Please forgive any minor typos below)

class Department \< ActiveRecord::Base  
has\_many :people  
end

class Person \< ActiveRecord::Base  
belongs\_to :department  
validates\_presence\_of :name  
end

@department = Department.new(:name=\>“bedrock”)  
@department.people \<\< Person.new(:name=\>“fred”)  
@department.people \<\< Person.new  
if @department.save

# redirect

else

# validation failed

# This is what actually happens in this example since one person

doesn’t have a name  
end

However if I update a person in the department with bad data and save,  
no errors are created and only the new department information is saved

# setup

@department = Department.new(:name=\>“bedrock”)  
@department.people \<\< Person.new(:name=\>“fred”)  
@department.save

# try to update with some bad data

@department = Department.find\_by\_name(“bedrock”)  
@department.name = “BEDROCK”  
@department.people[0].name = “”  
if @department.save

# This is what actually happens in this example eventhough there

should now be a name error.

# The new capitalized department name is stored in the database

# redirect

else

# validation failed

end

How can I get ActiveRecord to validate all the way into the person  
objects when updating? I tried the valid? method and that didn’t work  
either. It only checks the Department object.

Thanks,  
Peter

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [March 13, 2006, 5:17pm UTC](https://www.ruby-forum.com/t/validation-and-update/54690/2 "2006-03-13T17:17:34Z")

</div>

Hi,

I’ve been trying to figure this out but no luck.

I looked in the activerecord source at the save, create and update  
functions. In the case of create, I don’t see how activerecord knows  
to implicitly run a transaction with the parent and child tables. I  
don’t see any calls down to the child models. I’m glad that  
activerecord does this but I was hoping to see hwere update differs  
from create.

Any ideas?

Peter

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [March 13, 2006, 5:29pm UTC](https://www.ruby-forum.com/t/validation-and-update/54690/3 "2006-03-13T17:29:53Z")

</div>

What about \*validates\_associated?

\*If that doesn’t work, maybe your controller logic can perform a  
“valild?”  
call on your Person object…?

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [March 13, 2006, 5:48pm UTC](https://www.ruby-forum.com/t/validation-and-update/54690/4 "2006-03-13T17:48:38Z")

</div>

On 3/13/06, Michael G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What about validates\_associated?

Thanks Michael.

I guess I will have to build some controller logic to make sure that  
if the department and associated people all validate then the save  
happens as a transaction. I thought that the rails save would do this  
for me but it doesn’t seem to when using it to update records.  
Strange. If anyone else knows why save doesn’t I’m interested.

Thanks,  
Peter
