许多一对多连接和协会 [英] Many-to-many connection and Associations
问题描述
一个用户可以上传通过剪辑脚手架剪辑
,这个片段无论是属于显示
键,或专辑
(显示控制器,专辑控制器)。
A User can Upload a Clip via the Clips Scaffold
, this clip either belongs to a Show
and or an Album
(Show Controller, Album Controller).
一个用户可以有
- ONLY ONE SHOW
-
多张唱片
- ONLY ONE SHOW
MANY ALBUMS
我试图做到:
A 用户可以上传剪辑后,这些片段可以连接到任何一个显示或/和专辑
A User can Upload Clips, these clips can be attached to either a Show or/and an Album
有关更详细的介绍一起来看看下面的图片:
For a more detailed overview take a look at the image below:
问题1 什么我不理解是我必须使用关联。我目前的型号:
Problem #1 What i'm not understanding is the associations i must use. My current Models:
的用户模型的:
has_many :clips, dependent: :destroy
has_one :show, dependent: :destroy
has_many :albums, dependent :destroy
的夹型号的 belongs_to的:用户
的显示模式的 belongs_to的:用户
的相册样板的 belongs_to的:用户
---------------------------------------------------------------------------------
问题#2 我不知道如何来完成的,当用户上传的剪辑,他可以附加(或指定)到一个显示或专辑。
Problem #2 I'm not sure on how to accomplish, that when a user uploads a Clip, he can attach (or assign) it to a Show or Album.
我做了与显示(因为只有一个)只是调用<%= @ user.clip%>在View页面。
What i did with the Show (because there is only one) was just to call <%= @user.clip %> on the View Page.
这意味着,每个剪辑出用户上传的出现在显示。
这是错误的...
现在我有一个显示和专辑我要隆重用户的能力,以选择在哪里安装(在一在众多专辑他都有,或者他展)。
Now that i have a Show AND an Album i must grand the user the ability to choose where to attach it (On one of the many albums he have, or his Show).
或者我应该有一个解决方案,去那里的用户可以上传剪辑通过的相册或显示控制器(不通过单独的剪辑控制器)
Or should i go with a solution where the User can Upload a Clip THROUGH the Albums or Show controller (not through the separate Clips Controller)
我真的可以在这里需要一些帮助:)
推荐答案
您的问题#1是多态协会的教科书案例。
Your problem #1 is a textbook case of polymorphic association.
class User < ActiveRecord::Base
has_one :show,
:dependent => :destroy
has_many :clips,
:dependent => :destroy
has_many :albums,
:dependent => :destroy
end
class Clip < ActiveRecord::Base
belongs_to :user
belongs_to :attachable,
:polymorphic => true
end
class Show < ActiveRecord::Base
belongs_to :user
has_many :clips,
:as => :attachable
end
class Album < ActiveRecord::Base
belongs_to :user
has_many :clips,
:as => :attachable
end
您可以在的Rails指南了解更多关于多态关联。
You can learn more about polymorphic associations at Rails Guides.
现在您的问题#2,假设你做你的迁移按照你上面链接的引导必须在你的剪辑两台数据库领域
表:
Now for your problem #2, assuming you do your migrations as per the guide linked above you'll have two database fields in your clips
table:
-
attachable_id
- 这是ID
显示的实例
或专辑
那将收到的关联。 -
attachable_type
- 这是目标对象的类名,这样无论是显示
或专辑
。
attachable_id
- This is theid
of the instance ofShow
orAlbum
that's going to receive the association.attachable_type
- This is the class name of the target object, so eitherShow
orAlbum
.
在你上传表单,你要表现出节目和/或专辑的列表,并在 ID
分配给 attachable_id
和类名 attachable_type
。
In your upload form, you're going to show a list of shows and/or albums and assign the id
to attachable_id
and the class name to attachable_type
.
我会建议加载了你最初的选择只有一个类,然后通过JavaScript的情况下,替换它的内容,用户选择第二个选项。像这样:
I would suggest loading up your initial select with only one class and then replacing its contents via JavaScript in case the user selects the second option. Like so:
<%= f.collection_select :attachable_type, [['Show'], ['Album']], :first, :first, { :selected => 'Show' }, { :include_blank => false } %>
<%= f.collection_select :attachable_id, Show.all, :id, :name %>
这篇关于许多一对多连接和协会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!