Ваши ассоциации 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
Надеюсь, это немного поможет при настройке вашей модели.