如何创建报告类来处理用户滥用,虚假个人资料,不适当的照片?

pra*_*ski 2 ruby-on-rails ruby-on-rails-3

我希望我的用户能够报告其他用户,他们有虚假的个人资料,不合适的照片,使用侮辱性语言等.我正在考虑创建一个可以捕获此活动的Report类.我只是不确定这些协会.

例如,每个用户只能报告一次其他用户.但是很多用户可以报告给定的用户.我该如何实现呢?

Bil*_*han 7

您可以拥有与其他人具有多态关联的报表模型

class Report < ActiveRecord::Base
  belongs_to :reportable, polymorphic: true
  belongs_to :user
end

class Photo  < ActiveRecord::Base
  has_many :reports, as: :reportable
end

class Profile  < ActiveRecord::Base
  has_many :reports, as: :reportable
end

class User < ActiveRecord::Base
  has_many :reports                 # Allow user to report others
  has_many :reports, as: :reportable # Allow user to be reported as well
end
Run Code Online (Sandbox Code Playgroud)

您的reports表格将包含以下字段:

id, title, content, user_id(who reports this), reportable_type, reportable_id
Run Code Online (Sandbox Code Playgroud)

要确保用户只能报告一种类型的一个实例(假设用户只能报告一次其他用户的配置文件),只需在报告模型中添加此验证

validates_uniqueness_of :user_id, scope: [:reportable_type, :reportable_id]
Run Code Online (Sandbox Code Playgroud)

这些设置应该能够满足要求.

对于验证部分,感谢Dylan Markow在这个答案