开发者

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

开发者 https://www.devze.com 2023-03-30 06:59 出处:网络
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

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

精彩评论

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