Guest User

Untitled

a guest
Jan 22nd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. create_table "posts", :force => true do |t|
  2. t.integer "user_id"
  3. t.text "content"
  4. t.integer "upvotes", :default => 0
  5. t.integer "downvotes", :default => 0
  6. t.datetime "created_at", :null => false
  7. t.datetime "updated_at", :null => false
  8. end
  9.  
  10. create_table "votes", :force => true do |t|
  11. t.integer "user_id"
  12. t.integer "post_id"
  13. t.integer "vote", :default => 0
  14. t.datetime "created_at", :null => false
  15. t.datetime "updated_at", :null => false
  16. end
  17.  
  18. post_reputation = post.upvotes - post.downvotes
  19.  
  20. def calculate_post_reputation(post_id)
  21. some_post = Post.find(post_id)
  22. vote_count = 0
  23. some_post.votes.each do |vote|
  24. if vote.vote.to_i == 2
  25. vote_count += 1
  26. elsif vote.vote.to_i == 1
  27. vote_count -= 1
  28. end
  29. end
  30. vote_count
  31. end
  32.  
  33. Models:
  34.  
  35. class User < ActiveRecord::Base
  36. has_many :votes
  37. has_many :posts, :through => votes
  38.  
  39. class Post < ActiveRecord::Base
  40. has_many :votes
  41. has_many :users, :though => :votes
  42.  
  43. class Vote < ActiveRecord::Base
  44. belongs_to :user
  45. belongs_to :post
  46. attr_accessor :direction
  47. UP='Up'
  48. DOWN='Down'
  49. DIRECTIONS=[UP,DOWN]
  50. validates_inclusion_of :direction, in: [DIRECTIONS]
  51. scope :up_votes where(:direction => UP)
  52. scope :down_votes where(:direction => DOWN)
Add Comment
Please, Sign In to add comment