Ruby on rails 如何创建一个报告类来处理用户滥用、伪造个人资料、不合适的照片?

Ruby on rails 如何创建一个报告类来处理用户滥用、伪造个人资料、不合适的照片?,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我希望我的用户能够报告其他用户谁有伪造的个人资料,不适当的照片,使用辱骂性语言等。我正在考虑创建一个报告类,将捕获此活动。我只是不确定这些联系 例如,每个用户只能报告另一个用户一次。但是很多用户可以报告给定的用户。如何实现这一点?您可以拥有一个与其他报表模型具有多态关联的报表模型 class Report < ActiveRecord::Base belongs_to :reportable, polymorphic: true belongs_to :user end clas

我希望我的用户能够报告其他用户谁有伪造的个人资料,不适当的照片,使用辱骂性语言等。我正在考虑创建一个报告类,将捕获此活动。我只是不确定这些联系


例如,每个用户只能报告另一个用户一次。但是很多用户可以报告给定的用户。如何实现这一点?

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

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
为了确保一个用户只能报告一次一种类型的一个实例(比如一个用户只能报告另一个用户的配置文件一次),只需在报告模型中添加此验证

validates_uniqueness_of :user_id, scope: [:reportable_type, :reportable_id]
这些设置应能够满足要求


对于验证部分,由于

,您不必使用相同的名称进行关联。用户模型应该是这样的:
有很多:给定报告,类名:“报告”
validates_uniqueness_of :user_id, scope: [:reportable_type, :reportable_id]