Melbourne2991
Melbourne2991

Reputation: 11797

Better to spread across tables or use arrays for images and their attributes?

I am building my first rails application, and it's my first time dealing with any app of this level of complexity, I would like to know the most efficient/best way to design the database.

I have "thing" and each "thing" has a price as well as a number of images.

would it be better to have A:

a single table with 3 columns - id, price, images (being an array or hash storing properties such as width/height, url etc).

Or to have B:

two tables, one for "things" and one for "images", with the images columns being the various attributes? And then having the images belong_to "things"

Thanks

Upvotes: 0

Views: 64

Answers (2)

KenStipek
KenStipek

Reputation: 143

Go with B. On top of that check out these Gems for handling images:

https://github.com/thoughtbot/paperclip

https://github.com/carrierwaveuploader/carrierwave

And watch these RailsCasts for information on how to use them:

http://railscasts.com/episodes/134-paperclip

http://railscasts.com/episodes/253-carrierwave-file-uploads

You will want to have a one to many association from "things" to images, read more about how to do that here:

http://guides.rubyonrails.org/association_basics.html#the-has-many-association

And don't forget to store your price in pennies! :)

Upvotes: 0

Marek Lipka
Marek Lipka

Reputation: 51151

You should definitely choose B. You will have images as separate beings, despite being associated with things. It will be much simpler to maintain and expand this.

Upvotes: 1

Related Questions