1
我希望我的用户能够报告具有伪造配置文件,不适当的照片,使用辱骂性语言等的其他用户。我正在考虑创建一个可以捕获此活动的Report类。我只是不确定这些协会。如何创建报告类来处理用户滥用,虚假个人资料,不适当的照片?
例如,每个用户只能报告一次用户。但是很多用户可以报告给定的用户。我怎样才能实现这个?
我希望我的用户能够报告具有伪造配置文件,不适当的照片,使用辱骂性语言等的其他用户。我正在考虑创建一个可以捕获此活动的Report类。我只是不确定这些协会。如何创建报告类来处理用户滥用,虚假个人资料,不适当的照片?
例如,每个用户只能报告一次用户。但是很多用户可以报告给定的用户。我怎样才能实现这个?
你可以有一个报表模型多态性与他人
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
你reports
表将像场:
id, title, content, user_id(who reports this), reportable_type, reportable_id
要确保一个用户只能报一类的一个实例一次(假设用户只能报告一次用户的配置文件),只需在Report模型中添加此验证即可
validates_uniqueness_of :user_id, scope: [:reportable_type, :reportable_id]
这些设置应该能够满足要求。
进行验证部分,由于Dylan Markow at this answer
你不能有相同名称的关联。用户模型应该是这样的: 'has_many:given_reports,class_name:“Report”' – jokklan 2013-05-10 09:01:01