class Item < ActiveRecord::Base
	serialize :sections, Hash
	
	belongs_to :item_type
	belongs_to :origin, polymorphic: true

	has_many :user_votes, dependent: :destroy
	has_many :motion, dependent: :destroy
	has_and_belongs_to_many :wards

	accepts_nested_attributes_for :user_votes

	def next
		Item.where("id > ?", id).first 
	end

	def count_votes(vote)
		user_votes.where(vote: vote).count
	end

	def count_yes
		count_votes "Yes"
	end

	def count_no
		count_votes "No"
	end

	def count_skip
		count_votes "Skip"
	end

	def result
		if count_yes > count_no && count_yes > count_skip
			:carried
		elsif count_no > count_yes && count_no > count_skip
			:lost
		else
			:pending
		end
	end

end

class Item < ActiveRecord::Base
	serialize :sections, Hash
	
	belongs_to :item_type
	belongs_to :origin, polymorphic: true

	has_many :user_votes, dependent: :destroy
	has_many :motion, dependent: :destroy
	has_and_belongs_to_many :wards

	accepts_nested_attributes_for :user_votes

	def next
		Item.where("id > ?", id).first 
	end

	def get_user_votes_for(vote_type)
		votes = Array.new(0)

		user_votes.each { |user_vote| votes << user_vote if user_vote.vote == vote_type }

		votes
	end

	def count_yes
		get_user_votes_for("Yes").count
	end

	def count_no
		get_user_votes_for("No").count
	end

	def count_skip
		get_user_votes_for("Skip").count
	end

	def result
		if count_yes > count_no && count_yes > count_skip
			:carried
		elsif count_no > count_yes && count_no > count_skip
			:lost
		else
			:pending
		end
	end

end

Change Method Name, Count Votes To Get User Votes
Change Method Name, Count Votes To Get User Votes

Change the name and function of Count Votes from to Get User Votes
because the count votes is doing too many select statements to the
database. Get User Votes will find a participially vote type and return
an array of objects for the vote.
