Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load Binary Data From a File via ActiveRecord Create

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'))
like image 771
jerhinesmith Avatar asked Jun 05 '26 21:06

jerhinesmith


1 Answers

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.

like image 145
jerhinesmith Avatar answered Jun 08 '26 13:06

jerhinesmith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!