phi*_*ash 5 polymorphism ruby-on-rails has-and-belongs-to-many
在我的Rails应用程序中,我有三个模型,Projects,BlogPosts和Images.Projects和BlogPosts可以有许多链接的图像,图像可以链接到Project,BlogPost或两者.
在Rails中设置关联以使其工作的最佳方法是什么?
我将这个问题梳理成一个单独的模型类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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3417 次 |
| 最近记录: |