したがって、post_id によって投票が投稿に関連付けられる投稿モデルと投票モデルがあります。
投稿モデル
class Post < ActiveRecord::Base
attr_accessible :comment_count, :downvote, :id, :text, :title, :upvote, :url, :user_id, :users_voted_up_by, :users_voted_down_by
serialize :users_voted_up_by
serialize :users_voted_down_by
belongs_to :user
has_many :votes
end
投票モデル
class Vote < ActiveRecord::Base
attr_accessible :direction, :post_id, :type, :voter_id
belongs_to :user
belongs_to :post
belongs_to :comment
end
ループ内の現在の投稿Votes
を持つテーブル内のすべての行について、データベースにクエリを実行する必要があります。post_id
<% @posts.each do |post| %>
<%= Vote.count(:post_id, params[:post_id]) %>
<% end %>
しかし、それはすべての行をカウントするだけです。それらを関連付けるために何を書くことができますか?