In another question that i asked recently i got a really good answer and the code worked... But i do not know exactly why it works... Now i have a similar problem, but don't know how to solve it...?
What i have:
Models
users
questions (with answer_id)
answers
votes (with answer_id and user_id)
model for users:
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
model for questions:
belongs_to :user
has_many :answers, :dependent => :destroy
accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:text].blank? }, :allow_destroy => true
model for answers:
belongs_to :question
has_many :users, :through => :votes, :dependent => :destroy
has_many :votes
model for votes:
belongs_to :answer
belongs_to :user
In my question view i want to make a text bold when the current_used has voted on that specific answer. So how do i finish this:
<% 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