ActiveRecord and Images


#1

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
approach ?

TIA,
ngw

Nicholas W.
removed_email_address@domain.invalid


Yahoo! Messenger with Voice: chiama da PC a telefono a tariffe esclusive
http://it.messenger.yahoo.com


#2

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
approach ?
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:

http://jroller.com/page/bloritsch?entry=saving_images_to_the_database

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
images.

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
serious bottleneck.

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.


#3

Il giorno 08/mar/06, alle ore 05:08, Berin L. ha scritto:

CUT

Thank you very much, everything is much more clear now.


Nicholas W.
removed_email_address@domain.invalid


Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB
http://mail.yahoo.it


#4

Nicholas,

  • Next to what Berin told you, the simple/Rails way is to 1°/store
    images in the file system with 2°/ the file_column plugin.
  • As you’re asking for “best practices”, if you don’t have the user
    picture for a given account, I’d go for displaying a silhouette rather
    than showing a blank frame.

Alain