Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- create_table "posts", :force => true do |t|
- t.integer "user_id"
- t.text "content"
- t.integer "upvotes", :default => 0
- t.integer "downvotes", :default => 0
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
- end
- create_table "votes", :force => true do |t|
- t.integer "user_id"
- t.integer "post_id"
- t.integer "vote", :default => 0
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
- end
- post_reputation = post.upvotes - post.downvotes
- def calculate_post_reputation(post_id)
- some_post = Post.find(post_id)
- vote_count = 0
- some_post.votes.each do |vote|
- if vote.vote.to_i == 2
- vote_count += 1
- elsif vote.vote.to_i == 1
- vote_count -= 1
- end
- end
- vote_count
- end
- Models:
- class User < ActiveRecord::Base
- has_many :votes
- has_many :posts, :through => votes
- class Post < ActiveRecord::Base
- has_many :votes
- has_many :users, :though => :votes
- class Vote < ActiveRecord::Base
- belongs_to :user
- belongs_to :post
- attr_accessor :direction
- UP='Up'
- DOWN='Down'
- DIRECTIONS=[UP,DOWN]
- validates_inclusion_of :direction, in: [DIRECTIONS]
- scope :up_votes where(:direction => UP)
- scope :down_votes where(:direction => DOWN)
Add Comment
Please, Sign In to add comment