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 online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
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)