Ruby on rails Rails关联-用户通过评论拥有许多用户

Ruby on rails Rails关联-用户通过评论拥有许多用户,ruby-on-rails,associations,Ruby On Rails,Associations,我正试图设计一个评论系统,允许用户通过评论在其他用户的页面上发表评论 用户将在其页面上有一条评论,该评论由另一个名为“commenter”的用户发布 1) 以下代码是否合法/功能性/合理设计 2) 有一个重命名的“commenter”用户和一个未重命名的“user”用户一起使用可以吗?还是所有用户的关联名称都应该在语义上重命名 3) 是否有更好的方法来实现此设计意图(例如,不执行has_many to:) class用户

我正试图设计一个评论系统,允许用户通过评论在其他用户的页面上发表评论

用户将在其页面上有一条评论,该评论由另一个名为“commenter”的用户发布

1) 以下代码是否合法/功能性/合理设计

2) 有一个重命名的“commenter”用户和一个未重命名的“user”用户一起使用可以吗?还是所有用户的关联名称都应该在语义上重命名

3) 是否有更好的方法来实现此设计意图(例如,不执行has_many to:)

class用户
注:

我希望允许用户评论用户,但也可以评论其他模型(例如角色、名人)。因此,我认为需要将注释表用于各种各样的关联中

用户通过评论有很多评论 角色通过评论有许多评论者
名人通过评论有很多评论者

必须尝试作为可评论的宝石的行为,它提供了许多其他选择,以及公共、私人评论

必须尝试作为可评论的宝石的行为,它提供了许多其他选择,以及公共,私人评论

我相信你的设计不会像现在这样起作用-你在混合has_many和has_many。如果我是你,我会使用这样的方法:

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
class用户
我相信你的设计不会像现在这样起作用——你把很多东西和很多东西混在一起了。如果我是你,我会使用这样的方法:

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
class用户
我在mongoid和rails中实现了类似的功能。模型包括用户模型、友谊模型和请求模型。它的同类用户向另一个用户发送好友请求

class User
  include Mongoid::Document
  include Mongoid::Timestamps
  devise :invitable, :database_authenticatable, :registerable, :recoverable,
    :rememberable, :trackable, :validatable, :confirmable
  ...
  has_many :requests_from, class_name: "Request", inverse_of: :requested_by
  has_many :requests_to, class_name: "Request", inverse_of: :requested_to
  has_many :friendships, inverse_of: :owner

  def friends
    #retrive all the friendships and collect users have sent me a request or being sent a request.
    fs = Friendship.any_of({:friend_id.in => [self.id]}, {:owner_id.in => [self.id]}).where(state: 'accepted')
    User.in(id: fs.collect{|i| [i.friend_id, i.owner_id]}.flatten - [self.id])
  end
end#User

class Friendship
  include Mongoid::Document
  include Mongoid::Timestamps

  field :state, type: String, default: 'pending'
  field :pending, type: Boolean, default: true

  belongs_to :owner, class_name: 'User'
  belongs_to :friend, class_name: "User"

  validates :state, inclusion: { in: ["pending", "accepted", "rejected"]}
  ...
end#Friendship

class Request
  include Mongoid::Document
  include Mongoid::Timestamps

  field :state, type: String, default: 'pending'

  belongs_to :requested_by, class_name: 'User', inverse_of: :requests_from
  belongs_to :requested_to, class_name: 'User', inverse_of: :requests_to

  validates :state, inclusion: { in: ["pending", "accepted", "rejected"]}
  ...
end#Request

我希望这能有所帮助。

我在mongoid和rails中实现了类似的功能。模型包括用户模型、友谊模型和请求模型。它的同类用户向另一个用户发送好友请求

class User
  include Mongoid::Document
  include Mongoid::Timestamps
  devise :invitable, :database_authenticatable, :registerable, :recoverable,
    :rememberable, :trackable, :validatable, :confirmable
  ...
  has_many :requests_from, class_name: "Request", inverse_of: :requested_by
  has_many :requests_to, class_name: "Request", inverse_of: :requested_to
  has_many :friendships, inverse_of: :owner

  def friends
    #retrive all the friendships and collect users have sent me a request or being sent a request.
    fs = Friendship.any_of({:friend_id.in => [self.id]}, {:owner_id.in => [self.id]}).where(state: 'accepted')
    User.in(id: fs.collect{|i| [i.friend_id, i.owner_id]}.flatten - [self.id])
  end
end#User

class Friendship
  include Mongoid::Document
  include Mongoid::Timestamps

  field :state, type: String, default: 'pending'
  field :pending, type: Boolean, default: true

  belongs_to :owner, class_name: 'User'
  belongs_to :friend, class_name: "User"

  validates :state, inclusion: { in: ["pending", "accepted", "rejected"]}
  ...
end#Friendship

class Request
  include Mongoid::Document
  include Mongoid::Timestamps

  field :state, type: String, default: 'pending'

  belongs_to :requested_by, class_name: 'User', inverse_of: :requests_from
  belongs_to :requested_to, class_name: 'User', inverse_of: :requests_to

  validates :state, inclusion: { in: ["pending", "accepted", "rejected"]}
  ...
end#Request

我希望这会有所帮助。

有没有一个好地方可以让我看到它的功能、特点和缺点?即使你用谷歌搜索它,你也不会发现任何缺点,但它是一款非常流行的评论用的gem,它将为您提供最有效的服务。此外,act_as_comment gem使用模型名称,允许您根据您的评论在多个模型上使用相同的评论模型,以便在字符上使用评论表,名人你只需在你想要使用它的模型中添加act_as_commetable,其余的都会处理好,但gem本身有没有一个好地方可以让我看到它的功能、特点和缺点?即使你用谷歌搜索它,你也不会发现任何缺点,但它是一款非常流行的评论用的gem,它将为您提供最有效的服务。此外,act_as_comment gem使用模型名称,允许您根据您的评论在多个模型上使用相同的评论模型,以便在字符上使用评论表,名人你只需要在你想使用它的模型中添加act_as_commetable,其余的都会处理,但是gem本身我认为通常最好让“归属”模式包含拥有模型的密钥。我确实希望将其他属性添加到注释模型中,并使用注释模型作为其单独的实体,这就是我选择has_many through的原因。我还希望允许用户对其他表格进行评论,比如人物和名人,所以我希望能够容纳更多的设计。我将更新原始问题以反映这一点。我是否可以删除
has\u many:comments
,并保留其余内容为has\u many-through?从技术上讲,has\u many-through可以实现,只是我将其用于用户评论听起来有点奇怪。但你更了解你的应用程序将如何发展,所以这取决于你。如果你还想在其他模型中发表评论,你可以使用多态关联,例如在评论类
中属于:target,多态:true,其中目标是人物、名人等。我还得出了上一篇文章到现在之间多态评论的结论。=]“拥有的评论”和“发布的评论”是如何被识别的?您是否可以在属于某个用户的模型名称中添加任何带有下划线的名称,并在注释模型中不显式命名该新名称的情况下将其识别?我认为通常最好让“归属”模式包含所属模型的键。我确实希望将其他属性添加到注释模型中,并使用注释模型作为其单独的实体,这就是我选择has_many through的原因。我还希望允许用户对其他表格进行评论,比如人物和名人,所以我希望能够容纳更多的设计。我将更新原始问题以反映这一点。我是否可以删除
has\u many:comments
,并保留其余内容为has\u many-through?从技术上讲,has\u many-through可以实现,只是我将其用于用户评论听起来有点奇怪。但你更了解你的应用程序将如何发展,所以这取决于你。如果您还想在其他模型中进行注释,可以使用多态关联,例如