简体   繁体   English

表示各种模型之间关系的正确方式

[英]Proper way to represent the relationship between various models

Rails 5.1

rails generate scaffold Attendee screen_name:string tweets:integer

rails generate scaffold Follower followed_id:string attendee_id:string

rails generate scaffold Followed screen_name:string

When the application is running, a list is uploaded, with two fields:当应用程序运行时,会上传一个列表,其中包含两个字段:

screen_name
tweets

Part of the list upload form, is a field allowing the user to pick a name from the followeds table, causing two tables to be filled:列表上传表单的一部分,是一个允许用户从follows 表中选择一个名称的字段,从而导致填充两个表:

attendees

followers

The attendees table, will be filled with screen_name, and tweets, and the generated id, will be saved to the followers table, along with the id that was picked from the followeds table.与会者表将填入 screen_name,推文和生成的 id 将与从 follows 表中选取的 id 一起保存到关注者表中。

This means that each id from the attendees table, can have multiple entries in the followers table, and it could be part of an upload for multiple followeds ids.这意味着参加者表中的每个 id 可以在关注者表中有多个条目,并且它可以是多个被关注者 id 的上传的一部分。

My question is about the attendee.rb model:我的问题是关于参加者.rb 模型:

has_many :followers, through: :attendee_id_id

I am confused on how to describe the relationship with :followeds (or should I be using Followed?)我对如何描述与 :followeds 的关系感到困惑(或者我应该使用 Followed?)

rails generate scaffold Attendee screen_name:string tweets:integer

rails generate scaffold Follower followed_id:string attendee_id:string

rails generate scaffold Followed screen_name:string

if the above one is your requirement, then possible association of attendee model would be like this如果以上是您的要求,那么attendee模型的可能关联将是这样的

class Attendee
    has_many :followers
    has_many :followeds, through: :followers 
end

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

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