簡體   English   中英

加入Table Confusion Ruby on Rails

[英]Join Table Confusion Ruby on Rails

除了在Internet上散布的許多文章和文檔之外,我還閱讀了許多關於聯接表,STI表和多態關聯的問題和答案。 雖然我學到了很多東西,但我仍然困惑於在這種情況下應該做什么。 我可能已經看過答案了,但不知道自己正在看答案,但是我想看看是否有人可以幫助我了解我應該在這里做什么。

我有一個圖庫模型,一個相冊模型,一個圖像模型和一個類別模型。 這些都嵌套在用戶模型中。

創建相冊時,為其分配一個類別,這些類別將與一個Album_Categories模型一起保存。 我希望圖庫模型知道存在的類別,並能夠選擇要使用的類別。

選擇類別后,它應該能夠訪問與該類別關聯的相冊和通過“ Album_Images”聯接表鏈接的相冊圖像。 即使刪除了最初創建該相冊或圖庫的類別,該類別也應能夠繼續存在,以便以后其他相冊或圖庫可以利用它。

我的感覺是,只要創建了一個唯一的類別,就應該如何通過Category_Galleries模型將其連接到Gallery,但是在使用通過自己的特定聯接表連接到Gallery和Album的Images時,Gallery不會意識到Album_images Connection,因此,我認為共享由另一個創建的類別的知識是相同的。

任何幫助我理解的方法將不勝感激。

編輯:模型代碼

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

這實際上取決於您要建模的需求。 這是否准確反映了您的要求? (暫時忽略用戶,而不必詳細說明rails關聯)

  • 畫廊可以包含許多類別
  • 一個類別可以包含許多相冊
  • 相冊可以包含很多圖像

如果是這樣,您可以簡單地擁有:

  • 通過畫廊和類別之間的關聯建立has_many
  • 通過專輯和類別之間的關聯進行has_many
  • 通過相冊和圖像之間的關聯進行has_many

通過has_many,即使在關系被破壞后,您的類別,畫廊,相冊和圖像也可以存在。

目前,我看不到需要STI或多態。 通常,當兩個模型共享(擁有)同一張表時,您將使用多態關聯。 但是由於您將通過關聯使用has_many,所以甚至不需要多態。 (當在擁有的表中作為外鍵出現時,它可以防止擁有的表ID發生沖突)。

例如,要從圖庫中獲取圖像,您實際上將顯示屬於分配給圖庫的所有類別的所有專輯的所有圖像。 這可以通過關聯和查詢來完成。

因此,基本上,我不認為您的情況...根據我的理解...太復雜了,通過關聯進行has_many應該足夠了。

一個有趣的問題是用戶為什么與所有模型相關聯。 他們負責創建/關聯用戶的那些模型實例嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM