Question about images and models

So i am creating a website that will be displaying lots of images. I was hoping to use a database to index these images. Is this the best way to go about implementing this kind of feature? If so how do i associate the image in the assets directory with its corresponding ID in the database?

After doing some more research i found this site.
http://www.railszilla.com/ruby-rails-database-indexes/rails
would this be a good way to implement an image model?

1 thought on “Question about images and models”

  1. The `id` column in a sql database is always indexed, so you wouldn’t need to set one for your proposed `Image` model. The article you linked discusses adding indexes on `foreign_keys`, which references `id`’s in another table. For example using your images table:

    class AddUserRefToImages < ActiveRecord::Migration[5.0] def change add_reference :images, :user, foreign_key: true end end This migration will create a `user_id` column and appropriate index of the `users` table into the `images` table. This will give you the ability to then call something like this `image.user_id` to get the associated `user_id` for the corresponding image.

    Reply

Leave a Comment