Forum: Ruby on Rails Bug: collection#create misbehaves on validations

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.
Alder G. (Guest)
on 2006-06-05 00:54
(Received via mailing list)
So we have a Presentation model which belongs to the Conference model.
We don't want to create nameless Presentations, so we

class Presentation < ActiveRecord::Base
  belongs_to            :conference
  validates_presence_of :name, :on => :create
end

Now we have a conference instance called rails_conf. When we try to
create a new nameless Presentation through the association collection:

rails_conf.presentations.create()
#=> #<Presentation:0x39c5678 @errors=#<ActiveRecord::Errors:0x39af868
@errors={"name"=>["can't be blank"]}, @base=#<Presentation:0x39c5678
...>>, @attributes={"name"=>nil, "conference_id"=>3,
"abbrev_name"=>nil}, @new_record=true>

Ok, so the validation prevented the nameless Presentation from being
saved. However, it did insert a pseudo/defective Presentation instance
in the collection:

rails_conf.presentations
#=>[#<Presentation:0x39c5678 @errors=#<ActiveRecord::Errors:0x39af868
@errors={"name"=>["can't be blank"]}, @base=#<Presentation:0x39c5678
...>>, @attributes={"name"=>nil, "conference_id"=>3,
"abbrev_name"=>nil}, @new_record=true>]

Note that we get the same for 'validates_presence_of :name' sans the
':on => :create' part (i.e. for the default :on => :save).

This looks like a bug to me. We now have an invalid Presentation
instance, that cannot be saved and by all rights cannot exist, happily
living inside rails_conf.presentations. It would lead to further bugs,
since now rails_conf.presentations.empty? would return false, although
in any practical sense rails_conf does not have any presentations.

Did I miss anything, or should this be filed?
This topic is locked and can not be replied to.