用户必须通过评论很多用户

用户必须通过评论很多用户

本文介绍了Rails的协会 - 用户必须通过评论很多用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图设计一个评论系统允许用户张贴在其他用户的网页,通过征求意见。

I'm trying to design a comment system allowing users to post on other users' pages, through comments.

一个用户都会有自己的网页,这是由所谓的其他用户发布的注释批评家。

A user will have a comment on his page, which is posted by another user called "commenter."

1)在以下code合法/功能/设计得体?

1) Is the following code legit/functional/decent design?

2)是否确定有一个改名为评论者的用户,在使用未更名为用户的同时,还是应该的用户的所有关联的名字永远是语义改名?

2) Is it OK to have a renamed "commenter" user, in conjunction with an un-renamed "user", or should all association names of user always be renamed semantically?

3)是否有实现这种设计意图(例如不通过做的has_many更好的办法?)

3) Are there better ways to implement this design intent (e.g. not doing a has_many through:)?

class User < ActiveRecord::Base
  has_many :comments
  has_many :users,      through: :comments
  has_many :commenters, through: :comments, class_name: 'User'
end

class Comment < ActiveRecord::Base
  belongs_to :user
  belongs_to :commenter, class_name: 'User'
end

请注意:

我希望允许用户在用户评论,同时也对其他型号(例如人物,名人)。所以,我觉得有表可以在不同的has_many通过协会使用的意见被调用。

I wish to allow users to comment on users, but also on other models, (e.g. characters, celebrities). So I would think having the comments table be used in various has_many through associations is called for.

用户必须通过评论许多评论
人物有过评论许多评论
名人通过评论有许多评论

users has many commenters through commentscharacters has many commenters through commentscelebrities has many commenters through comments

推荐答案

我相信你的设计是行不通的,因为它是 - 你是通过混合的has_many用的has_many。如果我是你,我会用像这样的一种方法:

I believe your design is not going to work as it is - you are mixing has_many with has_many through. If I were you I would use an approach like this one:

class User < ActiveRecord::Base
  has_many :owned_comments, class_name: 'Comments', foreign_key: 'owner_id'
  has_many :posted_comments, class_name: 'Comments', foreign_key: 'commenter_id'
end

class Comment < ActiveRecord::Base
  belongs_to :owner, class_name: 'User'
  belongs_to :commenter, class_name: 'User'
end

这篇关于Rails的协会 - 用户必须通过评论很多用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 11:38