我正在使用Rails 4,並且希望獲得給定帖子的每個單獨的帖子評論的投票數。通過向下鑽取關係獲取計數
模式:
Post PostReview PostReviewVote
id post_id post_review_id
user_id user_id
voted_on_date
我有類似:
table.table.table-striped.table-hover
thead
tr
th Post Name
th Reviews Created For Post
th Total Votes in All Reviews For Post
tbody
- for post in @posts
tr
td= post.name
td= PostReview.where(post_id: post.id).size
td= PostReview.where(post_id: post.id).PostReviewVotes.size
但它不取,理由是運行時錯誤:
undefined method `PostReviewVotes' for #<ActiveRecord::Relation []>
有什麼建議?我的模型具有適當的關聯,僅供參考。
你說他們有適當的關聯。 「Post」是否具有關聯:'has_many:PostReviewVotes,through:PostReview'? – Beartech