简体   繁体   English

属于同一模型的2个模型之间的ActiveRecord关联

[英]ActiveRecord associations between 2 models that belong to the same model

Rails newbie, so bear with me here. Rails新手,所以在这里忍受我。 I need advice on how to properly set up the active record associations between 2 of my models. 我需要有关如何正确设置两个模型之间的活动记录关联的建议。

As it stands right now, I have a User model. 就目前而言,我有一个用户模型。 A User has_many Contacts. 用户has_many联系人。 And a User has_many Posts. 和一个用户has_many个帖子。 Simply put: 简单的说:

class User < ApplicationRecord
  has_many :posts, dependent: :destroy
  has_many :contacts, dependent: :destroy

class Contact < ApplicationRecord
  belongs_to :user

class Post < ApplicationRecord
  belongs_to :user

A user can send a post to one or more of their contacts (while that action is just something that triggers an email, I need to be able to track and display which contacts they've sent each post to). 用户可以向他们的一个或多个联系人发送帖子(尽管该操作只是触发电子邮件的操作,但我需要能够跟踪并显示他们已将每个帖子发送到的联系人)。 They can send a Post to many Contacts, and they may (over time) send each Contact more than 1 Post. 他们可以将帖子发送给许多联系人,并且(随着时间的推移)他们可以向每个联系人发送多于1个帖子。

I'm a little uncertain how to associate Post and Contacts. 我有点不确定如何关联邮寄和联系人。 Is it just a simple Post has_many Contacts (or vice versa)? 这仅仅是一个简单的has_many通讯录(反之亦然)吗? Or should it be that a Post has_many Contacts THROUGH a User? 还是应该有has_many通过用户联系的帖子?

While I'm normally one who learns by trial and error, this is one of those situations where I'm worried if I go to far down the wrong path here, I'll figure it out so late that it'll be painful to undo - so thanks for any advice. 虽然我通常是通过尝试和错误学习的人,但是在这种情况下,我担心如果我走错了路,我会很晚才弄清楚,这样会很痛苦。撤消-非常感谢您的任何建议。

I'm using Rails 5 for what it's worth. 我正在使用Rails 5来实现其价值。

What you are looking for is a many-to-many relationship. 您正在寻找的是多对多关系。 Since a post can be associated with many contacts, and a contact can be associated with many posts. 由于帖子可以与许多联系人关联,并且联系人可以与许多帖子关联。

Here is a great read for the many-to-many relationship 这是多对多关系的绝佳读物

Basically, you will be created a association table to help link the post and contacts table. 基本上,将创建一个关联表来帮助链接帖子和联系人表。

class Contact < ApplicationRecord
  belongs_to :user
  has_many :posts, through: post_contact

class Post < ApplicationRecord
  belongs_to :user
  has_many :contacts, throught: post_contact

You will be created a new join table called post_contact (or whatever you want) and that table provides the association between posts and contacts. 您将创建一个名为post_contact(或所需的任何名称)的新联接表,该表提供了帖子和联系人之间的关联。

Hope this helps. 希望这可以帮助。

you don't necessarily need a join between Post and Contacts , ie you could write @post.user.contacts or @contact.user.posts ,though it depends on your use-case. 您不一定需要在PostContacts之间建立连接,例如,您可以编写@post.user.contacts@contact.user.posts ,尽管这取决于您的用例。

But say you did want to create an association for Post to get all the comments of the same user. 但是说您确实想为Post创建一个关联以获取同一用户的所有评论。 You could write: 您可以这样写:

class Post < ActiveRecord::Base
  has_many :contacts_of_user, through: :user, source: :contacts
end

You can use this same logic to implement contact.posts as well. 您也可以使用相同的逻辑来实现contact.posts

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

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