0

I am building a photo gallery app in Rails (following this guide). I would like to expand on the app this guide builds by adding Devise user authentication and storing who uploads each photo. I have got Devise running nicely but I'm not sure what the best way to keep track of who uploads what. This is what I'm thinking:

class User < ActiveRecord::Base
  has_many :photos
  ...
end
class Photo < ActiveRecord::Base
  belongs to :user
  ...
end
class AddUserToPhotos < ActiveRecord::Migration
  def change
    add_column :photos, :user_id, :integer
  end
end

The above (as I understand it) would store the uploader in each photo record and as an added bonus, allow us to run @user.photos to see all of a users photos regardless of gallery.

Is this the best way to do this?

simonlehmann
  • 852
  • 1
  • 10
  • 27
  • 2
    Yes, this will do. – Emu Sep 04 '16 at 06:51
  • Yes but you should setup a foreign key and index on `photos.user_id`. If you run the migration generator as `rails g migration AddUserToPhotos user:belongs_to` it will create them for you. – max Sep 04 '16 at 07:22

0 Answers0