Railsアプリのページで3つの変数を定義しています。
if current_user
if Vote.where(:user_id => current_user.id, :post_id => post.id, :direction => 0).count > 0
active = ' upactive'
elsif Vote.where(:user_id => current_user.id, :post_id => post.id, :direction => 1).count > 0
active = ' downactive'
end
end
unless Vote.group(:post_id).where(:post_id => @posts.map(&:id), :direction => 0).count[post.id] == nil
upvotes = Vote.group(:post_id).where(:post_id => @posts.map(&:id), :direction => 0).count[post.id]
else
upvotes = 0
end
unless Vote.group(:post_id).where(:post_id => @posts.map(&:id), :direction => 1).count[post.id] == nil
downvotes = Vote.group(:post_id).where(:post_id => @posts.map(&:id), :direction => 1).count[post.id]
else
downvotes = 0
end
ifステートメントとunlessステートメントにかなりの繰り返しコードがあることに気付きました。0
上記のものと等しい3つの変数宣言を記述して、変数が常にの代わりになるようにするにはどうすればよいですかnil
。