Loading binary data from a file using ActiveRecord Create
Is there an easy (or generally accepted) way to load a binary column using the ActiveRecord create method?
For example, I'm trying to do something similar to this:
MyTableObject.create :name => "Test", :image => File.read("PathToMyFile.jpg")
+2
jerhinesmith
source
to share
1 answer
I was able to get this to work. Instead of 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.
+1
jerhinesmith
source
to share