Creating a Gallery in Rails

I am creating a simple website with a gallery. I have a photo model that has a page for each photo with its information and image. I do not know how to create a gallery in photographs.

The gallery model has several photos, the photo model has _and_belongs_to_many galleries. I thought about adding a gallery.title field to each page of the photo so that I have a list of photos for each gallery, and then display them in a view. Is this a good way to make a gallery?

(I looked at the code in some gallery apps on Github, but most of them are outdated, too complicated for my needs.)

+3
source share
1 answer

has_and_belongs_to_many , . , . :

class Album < ActiveRecord::Base
  has_and_belongs_to_many :photographs

class Photograph < ActiveRecord::Base
  has_and_belongs_to_many :albums

:

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

, .

+1

Source: https://habr.com/ru/post/1745091/


All Articles