Skip to content

Instantly share code, notes, and snippets.

@kirpachov
Created April 5, 2024 10:18
Show Gist options
  • Save kirpachov/af3fa5714327eb4ed6a9db70aaadb474 to your computer and use it in GitHub Desktop.
Save kirpachov/af3fa5714327eb4ed6a9db70aaadb474 to your computer and use it in GitHub Desktop.

Active storage

  • Does not save files to disk until commit.
Post.transaction do
  # This one is ok. Returns true.
  @post.attachments.attach(io: StringIO.new("something...."), filename: "new-post.txt", content_type: "plain/text")
  
  # This would raise ActiveStorage::FileNotFound error.
  # Plus, you won't find any new file in the filesystem until commit.
  # @post.attachments.last.download
end

# Meanwhile, outside the transaction (in case COMMIT), the file is available.
@post.attachments.last.download
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment