I have a model Comments
. Comments
has_many votes
. While displaying the comments
I have to to display the up or down arrow based on the votes given to the comments
by the user
. So in my erb file I have @comment
. @comment.votes
has all the votes
for that comments
irrespective of the user
. So i have to loop thru the@co开发者_开发知识库mment.votes
to check vote.user.id = @comment.user.id
. how can i achieve this in an erb file?
One approach would be to try a named_scope/scope on Vote, something like:
named_scope :for_user, lambda do |user_id|
:conditions => ["comments.user_id = (?)", user_id]
end
Which lets you do something like:
@comments.votes.for_user(current_user.id)
精彩评论