views:

85

answers:

1

I'm creating a simple site with a gallery. I have a photos model which has a page for each photo with its info and an image. I'm unsure how to create a gallery from the photos.

The gallery model has_many photos, the photos model has_and_belongs_to_many galleries. I thought of adding a gallery.title field on each photo page so I'd have a list of photos for each gallery then display them in a view. Is this a good way to make a gallery?

(I've looked through the code on some gallery apps on Github, but most are outdated are too complicated for my needs.)

+1  A: 

Your has_and_belongs_to_many associations should match up, so both Galleries and Photos should use that association. I've built a similar system recently, though mine revolves around albums. My models look like the following:

class Album < ActiveRecord::Base
  has_and_belongs_to_many :photographs

And:

class Photograph < ActiveRecord::Base
  has_and_belongs_to_many :albums

Your join table for the two would look like this:

class AlbumPhotographJoinTable < ActiveRecord::Migration
  def self.up
    create_table :albums_photographs, :id => false do |t|
      t.integer :album_id
      t.integer :photograph_id
    end
  end

  def self.down
    drop_table :albums_photographs
  end
end

Hope that helps a bit with your model setup.

Throlkim