开发者

Looping thru an array of objects to display content in an erb file

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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜