Rails 7.1: Unintended ActiveStorage behavior causing missing files during ActiveRecord callbacks
I'm trying to debug I'm relatively new to this, so bear with me. I'm working with an scenario with Rails 7.1 where files uploaded via ActiveStorage are sometimes not being saved correctly when I attempt to create a record with nested attributes. Specifically, I have a `Post` model that accepts nested attributes for an `Image` model, and during the `before_save` callback, the associated image attachments appear to be missing. This only happens under certain conditions, like when the posts are created in bulk. Hereβs a snippet of my models: ```ruby class Post < ApplicationRecord has_one :image, dependent: :destroy accepts_nested_attributes_for :image before_save :ensure_image_attached private def ensure_image_attached if image.attached? # Additional logic if needed else errors.add(:base, 'Image must be attached') throw(:abort) end end end class Image < ApplicationRecord belongs_to :post has_one_attached :file end ``` When I create a post with an image using the following code: ```ruby Post.create!({ title: 'My Post', image_attributes: { file: uploaded_file } }) ``` It works as intended, but when I try to create multiple posts in a loop: ```ruby Post.create!([{ title: 'Post 1', image_attributes: { file: uploaded_file1 } }, { title: 'Post 2', image_attributes: { file: uploaded_file2 } }]) ``` I get an behavior saying that the image must be attached, even though the images appear to be provided correctly. I've confirmed that `uploaded_file1` and `uploaded_file2` are valid `ActionDispatch::Http::UploadedFile` objects. I've also tried using `save!` instead of `create!`, but the scenario continues. Is there a known scenario with ActiveStorage and nested attributes in this context? How can I ensure that the images are correctly attached when creating multiple records simultaneously? For context: I'm using Ruby on Windows. Am I missing something obvious? Any advice would be much appreciated. This is happening in both development and production on Ubuntu 20.04.