在 Rails 上加入 Table 混乱 Ruby
Join Table Confusion Ruby on Rails
我在这里阅读了很多关于联接 table、STI table 和多态关联的问题和答案,此外还有许多文章和文档在 Internet 上传播。虽然我学到了很多东西,但我仍然对在我的情况下应该做什么感到困惑。我可能已经阅读了答案但不知道我正在阅读答案,但我想看看是否有人可以帮助我了解我应该在这里做什么。
我有一个 Gallery 模型、一个 Album 模型、一个 Image 模型和一个 Category 模型。这些都嵌套在用户模型中。
当您创建相册时,为其分配一个类别,这些类别将与 Album_Categories 模型一起保存。我希望 Gallery 模型知道存在哪些类别,并能够选择它想要使用的类别。
一旦它选择了一个类别,它应该能够访问与该类别关联的相册和相册的图像,它们通过 Album_Images 加入 table 链接。即使最初创建它的相册或画廊被删除,该类别也应该能够继续存在,以便以后可以使用另一个相册或画廊。
我的感觉是,无论何时创建一个独特的类别,都应该通过 Category_Galleries 模型连接到画廊,但在我使用图像时,它们通过自己的特定连接连接到画廊和相册 tables,图库不知道 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
这实际上取决于您尝试建模的要求。这是否准确反映了您的要求? (暂时忽略用户,不一定详细说明 rails 关联)
- 一个画廊可以包含很多类别
- 一个类别可以包含多个相册
- 一个相册可以有很多张图片
如果是这样,您可以简单地拥有:
- a has_many 通过画廊和类别之间的关联
- a has_many 通过相册和类别之间的关联
- a has_many 通过相册和图像之间的关联
has_many 通过将使您的类别、画廊、相册和图像即使在关系被破坏后仍然存在。
目前我没有看到任何需要 STI 或多态性。当两个模型共享(拥有)相同的 table 时,通常会使用多态关联。但是由于您将通过关联使用 has_many,因此甚至不需要多态性。 (当在拥有的 table 中作为外键出现时,它可以防止拥有的 table id 发生冲突)。
例如,要从图库获取图像,您基本上会显示属于分配给图库的所有类别的所有相册的所有图像。这可以通过关联和查询来完成。
所以基本上,我认为您的场景...根据我的理解...太复杂,has_many 通过关联就足够了。
一个有趣的问题是为什么用户与您的所有模型相关联。他们是否负责 creating/those 个用户关联的模型实例?
我在这里阅读了很多关于联接 table、STI table 和多态关联的问题和答案,此外还有许多文章和文档在 Internet 上传播。虽然我学到了很多东西,但我仍然对在我的情况下应该做什么感到困惑。我可能已经阅读了答案但不知道我正在阅读答案,但我想看看是否有人可以帮助我了解我应该在这里做什么。
我有一个 Gallery 模型、一个 Album 模型、一个 Image 模型和一个 Category 模型。这些都嵌套在用户模型中。
当您创建相册时,为其分配一个类别,这些类别将与 Album_Categories 模型一起保存。我希望 Gallery 模型知道存在哪些类别,并能够选择它想要使用的类别。
一旦它选择了一个类别,它应该能够访问与该类别关联的相册和相册的图像,它们通过 Album_Images 加入 table 链接。即使最初创建它的相册或画廊被删除,该类别也应该能够继续存在,以便以后可以使用另一个相册或画廊。
我的感觉是,无论何时创建一个独特的类别,都应该通过 Category_Galleries 模型连接到画廊,但在我使用图像时,它们通过自己的特定连接连接到画廊和相册 tables,图库不知道 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
这实际上取决于您尝试建模的要求。这是否准确反映了您的要求? (暂时忽略用户,不一定详细说明 rails 关联)
- 一个画廊可以包含很多类别
- 一个类别可以包含多个相册
- 一个相册可以有很多张图片
如果是这样,您可以简单地拥有:
- a has_many 通过画廊和类别之间的关联
- a has_many 通过相册和类别之间的关联
- a has_many 通过相册和图像之间的关联
has_many 通过将使您的类别、画廊、相册和图像即使在关系被破坏后仍然存在。
目前我没有看到任何需要 STI 或多态性。当两个模型共享(拥有)相同的 table 时,通常会使用多态关联。但是由于您将通过关联使用 has_many,因此甚至不需要多态性。 (当在拥有的 table 中作为外键出现时,它可以防止拥有的 table id 发生冲突)。
例如,要从图库获取图像,您基本上会显示属于分配给图库的所有类别的所有相册的所有图像。这可以通过关联和查询来完成。
所以基本上,我认为您的场景...根据我的理解...太复杂,has_many 通过关联就足够了。
一个有趣的问题是为什么用户与您的所有模型相关联。他们是否负责 creating/those 个用户关联的模型实例?