Skip to content

Instantly share code, notes, and snippets.

@akshaymohite
Last active July 19, 2017 12:18
Show Gist options
  • Save akshaymohite/e8e0743fea5b5710a656e944fe0bc3d8 to your computer and use it in GitHub Desktop.
Save akshaymohite/e8e0743fea5b5710a656e944fe0bc3d8 to your computer and use it in GitHub Desktop.
ActiveStorage: Image upload
user = User.create!(name: 'Akshay')
user.image.attach(io: File.open(Rails.root.join("public", "nature.jpeg")), filename: 'nature.jpg' , content_type: "image/jpg")
#(0.2ms) BEGIN
# SQL (0.4ms) INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "byte_size", "checksum", "created_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["key", "jd7L8dydnt11tXpsWWX9AXbw"], ["filename", "nature.jpg"], ["content_type", "image/jpg"], ["byte_size", 10055], ["checksum", "A5p01Zn3Rq3lG1gU8eji0w=="], ["created_at", "12:17:47.164687"]]
# (1.0ms) COMMIT
# (0.1ms) BEGIN
# SQL (0.3ms) INSERT INTO "active_storage_attachments" ("name", "record_gid", "blob_id", "created_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["name", "image"], ["record_gid", "gid://activestorage-demo/User/1"], ["blob_id", 2], ["created_at", "12:17:47.168074"]]
# (0.3ms) COMMIT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment