私は投票/アンケートアプリをレールに作成する方法を理解しようとしています。私は、ユーザーごとにPollVoteを追跡するにはどうすればよいポール/アンケートタイプのアプリをレールに組み込む方法
Poll (id, question:string, answer_1:string, answer_2:string, answer_3:string, answer_4:string, answer_5:string)
:
今私は、次のモデルがありますか?また、どのように私はそれが質問と回答(s)と投票を表示するフォームを構築するだろう。そして、ユーザーが投票したかどうかを確認するためにPollVoteモデルを照会しますか?
アイデア?
Response.count(:conditions => "question_id = #{@question.id} AND answer_id = #{@answer.id}")
編集:
class Poll < ActiveRecord::Base
has_many :questions
has_many :responses, :through => :questions
end
class Question < ActiveRecord::Base
belongs_to :poll
has_many :answers
has_many :responses, :through => :answers
end
class Answer < ActiveRecord::Base
belongs_to :question
has_many :responses
end
class Response < ActiveRecord::Base
belongs_to :user
belongs_to :answer
end
次に、あなたのようなことを行うことができます:ストレッチ
おかげ
ありがとうございましたが、特定の回答に投票した場合、質問、回答、ユーザーのフォームを表示するにはどうすればよいですか。それは表示する必要があります。彼らはまた、1つの質問につき1つの投票だけで、投票を変更することができます。アイデア? – AnApprentice
@AnApprentice私はいくつかのコードを追加して、実行可能な方法を示しました。あなたの考えを見てください。 – Luke
google for "rails counter_cache" ...これは、投票ごとの質問数、質問ごとの回答数、回答ごとの回答数などの高速クエリで役立ちます –