如何获得有多少用户收藏帖子的百分比?因此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 :(得分:1)
这将计算百分比并将其四舍五入至只有2个十进制数
$ valgrind ./bin/allocate_ptr_in_struct
==23024== Memcheck, a memory error detector
==23024== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==23024== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==23024== Command: ./bin/allocate_ptr_in_struct
==23024==
Inventory id : 0
Product[0]
subproduct[0][0] price : 2
subproduct[0][1] price : 4
Product[1]
subproduct[1][0] price : 4
subproduct[1][1] price : 8
Product[2]
subproduct[2][0] price : 6
subproduct[2][1] price : 12
Inventory id : 1
Product[0]
subproduct[0][0] price : 2
subproduct[0][1] price : 4
Product[1]
subproduct[1][0] price : 4
subproduct[1][1] price : 8
Product[2]
subproduct[2][0] price : 6
subproduct[2][1] price : 12
==23024==
==23024== HEAP SUMMARY:
==23024== in use at exit: 0 bytes in 0 blocks
==23024== total heap usage: 9 allocs, 9 frees, 128 bytes allocated
==23024==
==23024== All heap blocks were freed -- no leaks are possible
==23024==
==23024== For counts of detected and suppressed errors, rerun with: -v
==23024== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)