简体   繁体   English

滑轨| 链接共享应用| 数据库建模

[英]Rails | Link Sharing App | Database Modelling

I am trying to learn Rails by building a link sharing app. 我正在尝试通过构建链接共享应用程序来学习Rails。

1) User can submit a Post along with a reference to other users who have created that post. 1)用户可以提交帖子以及对创建该帖子的其他用户的引用。

2) So, a post belongs to a user(who submitted it) and also other users (who created the post) 2)因此,帖子属于一个用户(提交该帖子的用户)以及其他用户(创建该帖子的用户)

Currently, I have 2 tables 目前我有2张桌子

1) Users 2) Posts 1)用户2)帖子

class User < ActiveRecord::Base
  has_many :posts
end

class Post < ActiveRecord::Base
 belongs_to :submitted_user, class_name: 'User', foreign_key: 'user_id'
 #belongs to multiple other users who have created the post#
end

Should I use has_many through and create a join table PostCreators that has a user_id and post_id. 我是否应该使用has_many through并创建具有user_id和post_id的PostCreatorsPostCreators In that case Can I write multiple has_many :posts? 在那种情况下, Can I write multiple has_many :posts?

class User
 has_many :posts #submitted_user in post
 has_many :posts, through: :post_creators 
end

class Post 
  belongs_to :submitted_user, class_name: 'User', foreign_key: 'user_id'
  has_many :creators, through: :post_creators
end

class PostCreator
  belongs_to :user
  belongs_to :post
end
class User < ActiveRecord::Base
  has_many :user_posts
  has_many :posts, through: :user_posts
end

class Post < ActiveRecord::Base
  belongs_to :submitted_user, class_name: 'User', foreign_key: 'submitted_user_id'
  has_many :user_posts
  has_many :posts, through: :user_posts
end

class UserPost < ActiveRecord::Base
  belongs_to :user
  belongs_to :post
end

Also write a migration to add submitted_user_id to Post table 还编写一个迁移,以将Submitted_user_id添加到Post表

p = Post.last

p.users => will return array of created users p.users =>将返回创建的用户数组

p.submitted_user => will return submitter user object p.submitted_user =>将返回提交者用户对象

是的,您必须创建“发布”和“用户联接”表,并添加关联has_many,以在联接表上存储其他数据。

I have done it like this 我已经做到了

class User < ActiveRecord::Base
  has_many :user_posts
  has_many :posts, through: :user_posts
end

class Post < ActiveRecord::Base
  has_many :user_posts
  has_many :users, through: :user_posts
end

class UserPost < ActiveRecord::Base
  belongs_to :user
  belongs_to :post
  enum status: { submitter: 0, creators: 1 }
end

So, if I want to find out the creators of a post, I do 所以,如果我想找出帖子的创建者,我会

post = Post.first
UserPost.where(post: post).creators

By default, when a user submits a post, UserPost record of that user and post will be created with a enum status of 0. 默认情况下,当用户提交帖子时,该用户和帖子的UserPost记录将被创建,枚举状态为0。

When the submitter selects the post's creators, UserPost records will be created with that post, selected users and enum status of 1 当提交者选择帖子的创建者时,将使用该帖子,所选用户和枚举状态为1来创建UserPost记录。

I am not sure if this is the best way. 我不确定这是否是最好的方法。 Please comment if you think of an optimal way of doing this. 如果您认为这样做的最佳方式,请发表评论。

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

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