Reputation: 15492
Is there an easy (or generally accepted) way to load up a binary column using the create method of ActiveRecord?
For example, what I'm trying to do is something similar to this:
MyTableObject.create(name: 'Test', image: File.read('PathToMyFile.jpg'))
Upvotes: 3
Views: 1404
Reputation: 845
You can also use binread
https://ruby-doc.org/core-3.1.2/IO.html#binread-method
MyTableObject.create(name: 'Test', image: File.binread('PathToMyFile.jpg'))
Upvotes: 0
Reputation: 15492
I was able to get this working. Rather than doing:
MyTableObject.create(
name: 'Test',
image: File.read('PathToMyFile.jpg')
)
which did insert a record into the database but without the correct binary representation of the file
MyTableObject.create(
name: 'Test',
image: File.open('PathToMyFile.jpg', 'rb').read
)
seemed to do the trick.
Upvotes: 4