# Why inconsistent validation with .new?

**URL:** <https://www.ruby-forum.com/t/why-inconsistent-validation-with-new/166904>\
**Category:** Rails\
**Created:** [April 30, 2009, 7:46pm UTC](https://www.ruby-forum.com/t/why-inconsistent-validation-with-new/166904 "2009-04-30T19:46:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [April 30, 2009, 7:46pm UTC](https://www.ruby-forum.com/t/why-inconsistent-validation-with-new/166904/1 "2009-04-30T19:46:20Z")

</div>

(Rails 2.3.2, OS X 10.5, MySQL 5.0.x)

I’m seeing some odd behavior that makes no sense to me with respect to  
adding new empty records to an ActiveRecord object.

Assuming (shortened for email):

class Article \< AR::Base  
has\_many :pages  
end  
class Pages \< AR::Base  
belongs\_to :pages  
end

If I add a new Page to an Article (with no attribute data), then of  
course we would expect to see no validation errors because .new is not  
supposed to run through validation.

So, this works as expected (this is my literal short loop test code):

test\_object = Article.new  
test\_object.pages \<\< Page.new  
test\_offset = test\_object.pages.size - 1  
test\_errors = test\_object.pages[test\_offset].errors.size

# test\_errors -\> 0

However, this does not work as expected (literal short loop code for  
testing):

test\_object = Article.find(:first, :conditions =\> [“rcrd\_id = ?”,  
“abc123”])  
test\_object.pages \<\< Page.new  
test\_offset = test\_object.pages.size - 1  
test\_errors = test\_object.pages[test\_offset].errors.size

# test\_errors -\> 4, which correlates to expected errors if I tried to

save an empty record

# I am expecting to see 0

I have tested this on two completely different sets of models, and each  
shows the same behavior.

I have combed through all my callbacks, even commented them out. Nothing  
in there appears to be the problem (nothing forcing a save that I can  
see).

I have this short loop code inside a dead-simple controller index method  
with no other code that could interfere AFAICT.

This seems too obvious to be a Rails bug, so I am assuming it’s my code  
somehow, somewhere.

Any ideas on what else to look at?

– gw

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [April 30, 2009, 7:48pm UTC](https://www.ruby-forum.com/t/why-inconsistent-validation-with-new/166904/2 "2009-04-30T19:48:09Z")

</div>

Greg W. wrote:

> Assuming (shortened for email):
> 
> class Article \< AR::Base  
> has\_many :pages  
> end  
> class Pages \< AR::Base  
> belongs\_to :pages  
> end

Oops. Email typo – of course that should be:

class Pages \< AR::Base  
belongs\_to :article  
end

– gw

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [April 30, 2009, 8:16pm UTC](https://www.ruby-forum.com/t/why-inconsistent-validation-with-new/166904/3 "2009-04-30T20:16:13Z")

</div>

Frederick C. wrote:

> On Apr 30, 6:46ï¿½pm, Greg W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > (Rails 2.3.2, ï¿½OS X 10.5, MySQL 5.0.x)
> > 
> > I have combed through all my callbacks, even commented them out. Nothing  
> > in there appears to be the problem (nothing forcing a save that I can  
> > see).
> 
> “Adding an object to a collection (has\_many or  
> has\_and\_belongs\_to\_many) automatically saves that object, except if  
> the parent object (the owner of the collection) is not yet stored in  
> the database” ([http://api.rubyonrails.org/classes/ActiveRecord/](http://api.rubyonrails.org/classes/ActiveRecord/)  
> Associations/ClassMethods.html)
> 
> Fred

Most unfortuneate (but thanks).

– gw

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [April 30, 2009, 7:54pm UTC](https://www.ruby-forum.com/t/why-inconsistent-validation-with-new/166904/4 "2009-04-30T19:54:54Z")

</div>

On Apr 30, 6:46 pm, Greg W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> (Rails 2.3.2, OS X 10.5, MySQL 5.0.x)
> 
> I have combed through all my callbacks, even commented them out. Nothing  
> in there appears to be the problem (nothing forcing a save that I can  
> see).

“Adding an object to a collection (has\_many or  
has\_and\_belongs\_to\_many) automatically saves that object, except if  
the parent object (the owner of the collection) is not yet stored in  
the database” ([http://api.rubyonrails.org/classes/ActiveRecord/](http://api.rubyonrails.org/classes/ActiveRecord/)  
Associations/ClassMethods.html)

Fred
