简体   繁体   中英

Join Table Confusion Ruby on Rails

I have read a lot of the questions and answers here about join tables, STI tables, and polymorphic associations, in addition to many articles and documentation spread throughout the internet. While I've learned a lot I'm still confused about what I should do in my situation. I may have read the answer and not known I was reading the answer, but I wanted to see if someone could help me understand what it is I should do here.

I have a Gallery model, an Album model, an Image model and a Category model. These are all nested in a User model.

When you create an Album assign a Category to it and those are saved with an Album_Categories model. I want the Gallery model to be aware of what Categories exist and be able to choose which ones it would like to use.

Once it selects a Category, it should be able to access the Albums associated with the Category and the Album's Images, which are linked through and Album_Images join table. The Category should be able to continue to exist even if the Album or Gallery that it was originally created with is deleted so that another Album or Gallery can take advantage of it later.

My sense is that whenever a unique Category is created is should some how connect to Gallery through a Category_Galleries model, but in my use of Images which is connected to Gallery and Album with their own specific join tables, Gallery is unaware of an Album_images Connection, so I assume sharing the knowledge of a Category created by the other would be the same.

Any way to help me unerstand this would be appreciated.

Edit: model code

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

It really depends on the requirements you're trying to model. Does this accurately reflect your requirements? (ignoring the user for the current moment and not necessarily detailing rails associations)

  • A gallery can consist of many categories
  • A category can contain many albums
  • An album can have many images

If so, you could simply have:

  • a has_many through association between galleries and categories
  • a has_many through association between albums and categories
  • a has_many through association between albums and images

The has_many through will allow your categories, galleries, albums and images to exist even after relations are destroyed.

At the moment I don't see any need for STI or polymorphism. Usually you use polymorphic associations when two models share (own) the same table. But since you would use has_many through associations, polymorphism wouldn't even be necessary. (It prevents clashes of the owning table ids when occuring as a foreign key in the owned table).

To get to images, from gallery, for example you would be essentially displaying all the images of all albums belonging to all categories that are assigned to a gallery. That can be done through associations and querying.

So basically, I don't think your scenario...based on my understanding...is too complex and has_many through associations should suffice.

An interesting question would by why a user is associated to all of your models. Are they responsible for creating/those model instances a user is associated to?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM