ruby on rails - How can i get a percentage of how many users favorited a post -
how can percentage of how many users favorited post? 80% of users favorite first post. using gem called markable.
in posts controller can favorite post this.
class postscontroller < applicationcontroller def favorite @post = post.friendly.find(params[:id]) current_user.mark_as_favorite @post redirect_to @post end end
i can see users have favorited post this
@post = post.first << test post @post.users_have_marked_as_favorite << [user1, user2] @post.users_have_marked_as_favorite.count << 2
below post , user models
class post < activerecord::base extend friendlyid friendly_id :title, use: :slugged # markable_as :favorite gives me option favorite markable_as :favorite end class user < activerecord::base acts_as_marker end
this calculate percentage , round have 2 decimal numbers
class post < activerecord::base def favored_percentage (users_have_marked_as_favorite.count * 100 / user.count).round(2) end end
Comments
Post a Comment