2015-04-26 58 views
0

如何獲得有多少用戶收藏帖子的百分比?所以有80%的用戶喜歡第一篇文章。我也在使用名爲Markable的寶石。我如何獲得有多少用戶收到帖子的百分比

在我的帖子控制器我可以喜歡這樣的職位。

class PostsController < ApplicationController 
def favorite 
    @post = Post.friendly.find(params[:id]) 
    current_user.mark_as_favorite @post 
    redirect_to @post 
end 
end 

我可以看到所有誰收藏了一個帖子這樣

@post = Post.first << Test post 
@post.users_have_marked_as_favorite << [user1, user2] 
@post.users_have_marked_as_favorite.count << 2 

下面的用戶都是我的帖子和用戶模型

class Post < ActiveRecord::Base 
extend FriendlyId 
friendly_id :title, use: :slugged 
# the markable_as :favorite is what gives me the option to favorite 
markable_as :favorite 
end 

class User < ActiveRecord::Base 
acts_as_marker 
end 
+0

C你包括你的'用戶'和'Post'模型? –

回答

1

這將計算百分比,並將其輪只有2個十進制數字

class Post < ActiveRecord::Base 
    def favored_percentage 
    (users_have_marked_as_favorite.count * 100/User.count).round(2) 
    end 
end 
相關問題