ユーザーが、偽のプロファイル、不適切な写真、虐待的な言葉などを使用している他のユーザーを報告できるようにしたいと考えています。このアクティビティをキャプチャする Report クラスを作成することを考えていました。協会についてはよくわかりません。
たとえば、各ユーザーは別のユーザーを 1 回だけ報告できます。しかし、多くのユーザーが特定のユーザーを報告できます。どうすればこれを実装できますか?
ユーザーが、偽のプロファイル、不適切な写真、虐待的な言葉などを使用している他のユーザーを報告できるようにしたいと考えています。このアクティビティをキャプチャする Report クラスを作成することを考えていました。協会についてはよくわかりません。
たとえば、各ユーザーは別のユーザーを 1 回だけ報告できます。しかし、多くのユーザーが特定のユーザーを報告できます。どうすればこれを実装できますか?
他のモデルとのポリモーフィックな関連付けを持つレポート モデルを持つことができます
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
ユーザーが 1 つのタイプの 1 つのインスタンスを 1 回だけ報告できるようにするには (たとえば、ユーザーは別のユーザーのプロファイルを 1 回しか報告できないとします)、この検証を Report モデルに追加するだけです。
validates_uniqueness_of :user_id, scope: [:reportable_type, :reportable_id]
これらの設定は、要件を満たすことができる必要があります。
検証部分については、この回答の Dylan Markowに感謝します