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

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and 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

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

#=> #<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:

#=>[#<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.