2010-05-12 61 views
3

我正在創建一個帶有畫廊的簡單網站。我有一個照片模型,其中包含每張照片的信息和圖像。 我不確定如何從照片創建一個畫廊。在Rails中創建畫廊

圖庫模型has_many照片,照片模型has_and_belongs_to_many畫廊。 我想在每張照片頁面上添加一個gallery.title字段,這樣我就可以獲得每個圖庫的照片列表,然後在視圖中顯示它們。 這是一個製作畫廊的好方法嗎?

(我已經通過在Github上一些畫廊的應用程序的代碼看,但大多數是過時的是我的需求太複雜了。)

回答

1

你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 

希望有點幫助與您的模型設置。