Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I store an image in a database using Ruby and Sequel?

I am using Ruby, or more specifically the Ramaze framework with the Sequel ORM. So please don't tell me about the Paperclip gem, as it is only compatible with ActiveRecord.

With that in mind, how can I store an image in a database using Ruby and Sequel?

like image 224
desbest Avatar asked Dec 20 '22 15:12

desbest


1 Answers

require "sequel"

DB = Sequel.sqlite

DB.create_table :images do
  primary_key :id
  String :name
  File :data
end

images = DB[:images]
images.insert(name: 'foo', data: Sequel.blob(File.read('/mydir/myimage.jpg')))
like image 171
Mori Avatar answered Jan 01 '23 09:01

Mori