Polymorphic associations and Validation

Hello there!

I have a question about polymorphic associations. Suppose we have a
parent class Parent and a child class Child, to create a new object I
do something like:

class Child …
:has_one :parent, :as => :resource
:validates_presence_of :name

end

class Parent …
:belongs_to :resource, :polymorphic => true

end

child = Child.new
parent = ParentClass.new
parent.resource = child
parent.save!

but when validation of Child fails, rails still try to execute “insert
into parents (…, resourse_id) … (…, NULL)”, and because my db
does not allow resource_id to be NULL, it raises a StatementInvalid
exception instead of raising RecordInvalid (as it should be if the
validation fails).

Am I doing something wrong or it’s just a bug of rails?

Thanks.