Alder Green
2006-Jun-04 20:52 UTC
[Rails] Bug: collection#create misbehaves on validations
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?
--
-Alder
Seemingly Similar Threads
- create separate plots by factors
- Emu48 under wine, misbehaves
- collection.build or collection.create gives "ArgumentError: wrong number of arguments (1 for 0)"
- R-alpha: model.matrix misbehaves on two-sided formulas
- Why does belongs_to handle "conflicts" this way?
