ここでは、インターネット上に広がる多くの記事やドキュメントに加えて、ジョインテーブル、STIテーブル、多態的な関連付けに関する多くの質問と回答を読んでいます。私は多くを学んだが、私は自分の状況で何をすべきかについて、まだ混乱している。私は答えを読んでいるかもしれませんが、私は答えを読んでいましたが、誰かが私がここですべきことを理解するのを助けることができるかどうかを見たいと思っていました。Join Table Confusion Ruby on Rails
私はギャラリーモデル、アルバムモデル、画像モデル、カテゴリモデルを持っています。これらはすべてUserモデルにネストされています。
アルバムを作成するときにカテゴリを割り当てると、Album_Categoriesモデルで保存されます。私はギャラリーモデルがどのようなカテゴリーが存在するかを認識し、どのカテゴリーを使用したいかを選択できるようにしたい。
カテゴリを選択すると、カテゴリに関連付けられたアルバムと、アルバムの画像がリンクされ、Album_Imagesの結合テーブルにアクセスできるようになります。カテゴリは、最初に作成されたアルバムまたはギャラリーが削除されても、別のアルバムまたはギャラリーで後で利用できるようにしても、引き続き存在することができます。
私の感覚は、一意のカテゴリが作成されると、Category_Galleriesモデルを介してGalleryに接続する方法が異なりますが、GalleryとAlbumにそれぞれ独自の結合テーブルを使用して接続されているImageを使用する場合、Galleryは認識しませんAlbum_images接続なので、他の人が作成したカテゴリの知識を共有することは同じであると思います。
私がこれを理解できないようにするための方法はありがたいです。
編集:モデルコード
class User < ActiveRecord::Base
has_many :images, dependent: :destroy
has_many :galleries, dependent: :destroy
has_many :albums, dependent: :destroy
has_many :categories, dependent: :destroy
accepts_nested_attributes_for :images, :galleries, :albums, :categories, allow_destroy: true
accepts_attachments_for :images, attachment: :file, append: true
end
class Image < ActiveRecord::Base
belongs_to :user
has_many :gallery_images, dependent: :destroy
has_many :galleries, through: :gallery_images
has_many :album_images, dependent: :destroy
has_many :albums, through: :album_images
attachment :file, type: :image
validates :file, presence: true
end
class Album < ActiveRecord::Base
belongs_to :user
validates :user_id, presence: true
has_many :album_galleries
has_many :galleries, through: :album_galleries # , dependent: :destroy
has_many :album_images, dependent: :destroy
has_many :images, through: :album_images
has_many :album_categories
has_many :categories, through: :album_categories
accepts_attachments_for :images, attachment: :file, append: true
accepts_nested_attributes_for :images
end
class Gallery < ActiveRecord::Base
belongs_to :user
validates :user_id, presence: true
has_many :gallery_images, dependent: :destroy
has_many :images, through: :gallery_images
has_many :album_galleries, dependent: :destroy
has_many :albums, through: :album_galleries
accepts_attachments_for :images, attachment: :file, append: true
accepts_nested_attributes_for :images
end
class Category < ActiveRecord::Base
belongs_to :user
validates :user_id, presence: true
has_many :albums, through: :album_categories
has_many :album_categories
end
class GalleryImage < ActiveRecord::Base
belongs_to :gallery
belongs_to :image
end
class AlbumCategory < ActiveRecord::Base
belongs_to :category
belongs_to :album
end
class AlbumGallery < ActiveRecord::Base
belongs_to :gallery
belongs_to :album
end
class AlbumImage < ActiveRecord::Base
belongs_to :album
belongs_to :image
end
モデルのコード、具体的には関連付けの定義を追加してください。 –
ギャラリーには独自の画像セットがありますか、それとも関連するすべてのアルバム画像によって推測されますか? – hypern
カテゴリがギャラリーに関連付けられ、カテゴリがアルバムに関連付けられている場合、ギャラリーのすべてのカテゴリのすべてのアルバムのすべての画像をチェックすることによって、ギャラリーに代わって関連付けられたすべての画像を見つけることができます。 – hypern