medihack/make_voteable — Issue #12: Cron job breaking votes?
Author: rapcal
Created: 2011-10-25 05:20:18+00:00
State: open

I'm facing a very strange bug. Actually I'm not even sure it's related to make_voteable. Also, I'm not sure this is the appropriate place to discuss this, so feel free to close/delete/ignore the thread as you see fit. I just ran out of options and can't figure out what's going on.

My application has Projects. Projects have statuses. They start as "Developing" and after the set development deadline is reached, they change to "Pending voting", when users can vote them up or down (until the voting deadline is reached) to prioritize them.

So if the deadline == "Pending voting", the views show links to the following actions on my projects controller (PUT):

def vote_yes
@project = Project.find(params[:id])
current_user.up_vote(@project)
flash[:success] = 'Thanks for voting!'
redirect_to @project
rescue MakeVoteable::Exceptions::AlreadyVotedError
flash[:error] = 'You already voted!'
redirect_to @project
end

def vote_no
@project = Project.find(params[:id])
current_user.down_vote(@project)
flash[:success] = 'Thanks for voting!'
redirect_to @project
rescue MakeVoteable::Exceptions::AlreadyVotedError
flash[:error] = 'You already voted!'
redirect_to @project
end

Everything works great if I update the deadline/status/voting deadline through the terminal. Voting works as expected.

However (and here comes the bug after the long intro...) if I run the cron job I setup to take care of this, the views render the voting links, users can click as usual, but _no vote is stored on the projects table_. The votings table, however, gets populated normally...

The only thing the cron job does is run this bit of code from the Project model:

def self.close_projects
@finished_projects = Project.where('deadline < ? and status = ?', Time.now.utc, "Developing")
@finished_projects.each do | project |
project.update_attribute(:status, "Pending voting")
project.update_attribute(:voting_deadline, Time.now.utc + 1.week )
end
end

Just an observation: I ran a migration to add the voting_deadline column to the Project table after running the migration that added the up/down_votes to the same model. I don't think this should be an issue, but maybe it's worth noticing.