Forum: Ruby on Rails Why inconsistent validation with .new ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Greg W. (Guest)
on 2009-04-30 21:46
(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
Greg W. (Guest)
on 2009-04-30 21:48
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
Frederick C. (Guest)
on 2009-04-30 21:54
(Received via mailing list)
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
Greg W. (Guest)
on 2009-04-30 22:16
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
This topic is locked and can not be replied to.