Nicholas W. wrote:
Not really a Rails question, but a “best practices” one.
If I want to associate an image to every account in my application, is
it better to insert the image file directly inside the db or to upload
the image in public/images/ ?
Also, if I want to associate a cover to a movie, what’s the best
As with all good design question the answer is, “it depends”
Most of the file upload plugins and help you find for Rails is built
around the filesystem, so for ease of use sake that makes sense. If you
want a guide for how to work directly with the database I posted
instructions on my blog here:
Now, to answer your question. The whole “it depends” answer hinges on
how you plan on growing. The Rails model of growth is to add more
machines to handle the load. It’s a reasonable way to go, as evidenced
by Google, Yahoo, etc. As you add more boxes you need to get at the
If you stored your images in the database, as your ISP handles upgrading
the database (usually with a SAN or something) you have to change
nothing. It’s all there, ready and waiting.
If you stored your images in the filesystem, you have a new challenge.
Either you need to use a networked drive to house the application and
let other machines read that drive, or you need to use a SAN for the
application. The issue here is throughput. If you have a dozen
machines all trying to read and write files from one, then you have a
In reality, by the time you need to start distributing load across
multiple machines a SAN is probably in your near future. (SAN: Storage
Area Network) Whether its for the database or the file system, SANs can
distribute your storage needs across multiple physical locations and
look like one really huge drive. All of the clustering and distributing
is done behind the scenes.
My personal opinion is that storing the images in the database gives me
that extra level of security blanket feeling. Its transactional (with
the exception of certain legacy table types in MySQL), which means I
never get a partially saved file. It’s also usually easy to back up and
restore as those are functions are built into the database anyway.
Lastly, all my data is in one place so it makes it easier for me to
manage. As with all things YMMV.