简体   繁体   English

在Rails中的3个表之间建立数据库连接

[英]setting up database connections between 3 tables in rails

i can't seem to find a straight forward answer so let me explain what i'm trying to do with my example: 我似乎找不到直截了当的答案,所以让我解释一下我想用我的例子做什么:

I'm trying to connect 3 tables together, i have Users Artists and Events 我正在尝试将3张桌子连接在一起,我有Users ArtistsEvents

Now what im trying to do is make a huge spiderweb here, the plan is to have the user follow the artist and/or event. 现在我想做的是在这里制作一个巨大的蜘蛛网,计划是让用户关注艺术家和/或事件。

The artist belongs to an event (there can be many artists to an event) 艺术家belongs to一个事件(一个事件可以有很多艺术家)

This is where i start to get confused. 这就是我开始感到困惑的地方。 How do i go about making this work so that the user is connected to the event/artist depending on who/what they follow? 我该如何进行这项工作,以便使用户根据其关注的对象/对象与活动/艺术家建立联系? (this is done in the view, the user goes to the event page and can click a stay updated button which would then save either the eventid or artist id to be used later. (这是在视图中完成的,用户可以转到事件页面,然后单击“ stay updated按钮,然后保存事件ID或演出者ID,以备后用。

What i don't get is, 我不明白的是

What columns do i need to make in the database, 我需要在数据库中填写哪些列,

What do i need to do to the models to link them all together? 我需要对模型做些什么来将它们链接在一起?

All my models are currently empty. 我所有的模型目前都是空的。

This is a has_many :through association. 这是一个has_many :through关联。 The Rails Associations guide is very detailed on this topic, so check it out for an in-depth explanation. Rails协会指南对此主题非常详细,因此请查看它以获取详细说明。

Let's start with the Artists and Events tables. 让我们从ArtistsEvents表开始。

An artist can play at many events, and an event will have many artists. 一个艺术家可以参加许多活动,一个活动将有许多艺术家。 To implement this relation a third table called EventArtists which keeps track of which events have which artists is needed, and which artists play at which events. 为了实现这种关系,请使用名为EventArtists的第三张表,该表跟踪哪些事件需要哪些艺术家,以及哪些艺术家在哪些事件中播放。

You can have multiple artists for one event (EventID 1), and a repeating artist (ArtistID 1) participating at multiple events. 一个事件(EventID 1)可以有多位艺术家,而多个事件可以有一位重复艺术家(ArtistID 1)。

EXAMPLE: 例:

EventsArtists 
-----------------
ArtistID EventID
1        1
2        1
1        2

Your EventArtists will have a minimum of the two columns listed in the table above, which are ArtistID and EventID . 您的EventArtists至少具有上表中列出的两列,即ArtistIDEventID

Your models will then look like the following. 然后,您的模型将如下所示。

class Artist < ApplicationRecord
has_many :event_artists
has_many :events, through: :event_artists

class Event < ApplicationRecord
has_many :event_artists
has_many :artists, through: :event_artists

class EventArtist < ApplicationRecord
belongs_to :artist
belongs_to :event

To add a Users table into the mix, you must create UserArtists and UserEvents . 要将Users表添加到组合中,必须创建UserArtistsUserEvents

Note : Table names such as UserArtists could be instead be named Follows - it all depends on what you think is clear. :表的名称,如UserArtists可以代替被命名Follows -这一切都取决于你的想法是明确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM