rubyimageblobsequelramaze

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?


Solution

  • 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')))