2

複数のモデル/クラスにポリモーフィックに適用できるコンテンツ投票メカニズムをどのように設計しますか? (Ruby on Rails のコンテキストが望ましいですが、他のコンテキストでも問題ありません)

これらのクラスのインスタンスが投票できる場合: - 記事 - 質問 - 製品

有権者は登録を要求されるべきではありません。

投票者をオブジェクトごとに 1 票に制限するように最善の努力を払う必要があります。(特定の記事に 1 票、特定の質問に 1 票など)。つまり、IP 検出、Cookie などを使用します。

4

3 に答える 3

2

「投票可能な」インターフェイスの単一テーブル継承から始めて、そこから投票可能なクラスを派生させることをお勧めします。STI の開始の詳細: http://wiki.rubyonrails.org/rails/pages/singletableinheritance

于 2008-11-04T16:04:16.517 に答える
1

あなたは多形的に言いました-それがキーワードです。

ActsAsTaggable がどのように機能するかを見てください。特定のモデルに投票動作を提供する ActsAsVotable 「プラグイン」を作成できます。投票は、ポリモーフィックな belongs_to を持つ別のモデル (Vote?) に保存されます。

create_table :orders do |t|
  t.string :votable_type
  t.integer :votable_id
  t.integer :vote
  t.timestamps
end

あなたのacts_as_votable呼び出しは舞台裏でそれをその投票に関連付けます:

has_many :votes, :as => "votable"

繰り返しますが、ActsAsTaggable を参照してください。:)

于 2008-11-06T14:26:02.923 に答える
0

Thanks for the answers guys. Yup, creating a polymorphic votable model is the way to go, however I was attempting to extract a more complete answer.

For example here's what I'm thinking so far. A voteable class which can be tied to multiple classes (Article, Question, Product) in this scenario.

Votes table: id:integer, vote:boolean, voteable_type:string (name of the class being voted on), voteable_id:integer (id of the class being voted on), voter_id (identifer for the user voting).

Now we still need to try and limit votes to one per user without requiring some sort of registration. So the voter_id could be a composite containing the user's IP address and user_agent. This is where I'm open to ideas. The IP address captured is not necessarily unique if they're coming from a corp proxy etc. so combining the user agent shoud help somewhat. However you still have cases where two user's can have the same IP and user agent string. Also, you may not always be able to obtain this information if their browser doesn't send it in the request header, meaning that some user's simply won't get to vote... Also, IPs can be spoofed.

Another possiblity would be using cookies, but I think this invites abuse from user's who want to spike the votes for a particular piece of content by clearing their cookies or scripting hits. Checking the user-agent here should help too, but you can fake a user-agent via script easily enough.

There probably is no perfect solution, but I'm curious how other's have approached this. I.e. hacker news and even stackoverflow.

于 2008-11-07T14:26:19.913 に答える