私が最近尋ねた別の質問では、私は本当に良い答えを得て、コードは機能しました...しかし、なぜそれが機能するのか正確にはわかりません...今、私は同様の問題を抱えていますが、それを解決する方法がわかりません。 。?
私が持っているもの:
モデル
users
questions (with answer_id)
answers
votes (with answer_id and user_id)
ユーザー向けモデル:
has_many :questions
has_many :votes
def can_vote_on? (question)
!question.answers.joins(:votes).where('votes.user_id = ?', id).exists?
end
def voted_answer? (question)
(what to do here...?)
end
質問のモデル:
belongs_to :user
has_many :answers, :dependent => :destroy
accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:text].blank? }, :allow_destroy => true
回答のモデル:
belongs_to :question
has_many :users, :through => :votes, :dependent => :destroy
has_many :votes
投票のモデル:
belongs_to :answer
belongs_to :user
私の質問ビューでは、current_usedがその特定の回答に投票したときに、テキストを太字にしたいです。だから私はこれをどのように終えるのですか?
<% for answer in @question.answers %>
<% if current_user.voted_answer? (@question) %>
<td>
<strong><%= answer.text %></strong>
</td>
<% else %>
<td>
<%= answer.text %>
</td>
<% end %>
<% end %>
Thijs