Read more

has_one association may silently drop associated record when it is invalid

Dominik Schöler
July 13, 2016Software engineer at makandra GmbH

This is quite an edge case, and appears like a bug in Rails (4.2.6) to me.

Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

Update: This is now documented on Edgeguides Ruby on Rails Show archive.org snapshot :

If you set the :validate option to true, then associated objects will be validated whenever you save this object. By default, this is false: associated objects will not be validated when this object is saved.

Setup

# post.rb
class Post < ActiveRecord::Base
  has_one :attachment
end
# attachment.rb
class Attachment < ActiveRecord::Base
  belongs_to :post
  validates :title, presence: true
end

Issue

When creating a post with an invalid attachment, I expect the creation to fail because of attachment validation errors. However, the attachment is silently not stored to the database.

post = Post.create!(attachment: Attachment.new)
post.id # => 123
post.attachment.id # => nil

This only happens …

  • when relying on autosave. Explicitly setting autosave: true fixes this. Also, adding validate: true to has_one :attachment fixes this.
  • with has_one. When it's has_many :attachments, validation errors of the attachment properly cancel the creation of the post.
  • when creating both post and attachment at the same time. Updating an already persisted post with an invalid attachment cancels the update.
Posted by Dominik Schöler to makandra dev (2016-07-13 09:17)