Why inconsistent validation with .new?


#1

(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


#2

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


#3

Frederick C. wrote:

On Apr 30, 6:46�pm, Greg W. 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/
Associations/ClassMethods.html)

Fred

Most unfortuneate (but thanks).

– gw


#4

On Apr 30, 6:46 pm, Greg W. 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/
Associations/ClassMethods.html)

Fred