【发布时间】:2011-04-05 10:49:10
【问题描述】:
在我最近提出的另一个问题中,我得到了一个非常好的答案并且代码有效...但我不知道它为什么有效...现在我有一个类似的问题,但不知道如何解决它...?
我有什么:
型号
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 %>
提斯
【问题讨论】:
标签: ruby ruby-on-rails-3 rails-models