【发布时间】:2008-11-02 23:46:04
【问题描述】:
在我的 Rails 应用程序中,我有三个模型,项目、博客帖子和图像。项目和博客帖子可以有许多链接的图像,并且一个图像可以链接到一个项目、一个博客帖子或两者。
在 Rails 中建立关联以使其工作的最佳方式是什么?
【问题讨论】:
标签: ruby-on-rails polymorphism has-and-belongs-to-many
在我的 Rails 应用程序中,我有三个模型,项目、博客帖子和图像。项目和博客帖子可以有许多链接的图像,并且一个图像可以链接到一个项目、一个博客帖子或两者。
在 Rails 中建立关联以使其工作的最佳方式是什么?
【问题讨论】:
标签: ruby-on-rails polymorphism has-and-belongs-to-many
我会将 habtm 梳理成一个单独的模型类 ImageLink。然后你会得到:
Project
has_many :image_links, :as => :resource
BlogPost
has_many :image_links, :as => :resource
ImageLink
belongs_to :image
belongs_to :resource, :polymorphic => true
Image:
has_many :image_links
【讨论】: