Question

I'm facing a very strange bug with the make_voteable gem. Actually I'm not even sure it's related to make_voteable or if it's a rake issue.

My application has a Project model. Projects have a status attribute. They start as "Developing" and after the set development deadline is reached, the status changes to "Pending voting". Then users can vote the projects up or down (until the voting deadline is reached) to prioritize them.

So if the status for a project is "Pending voting", the project show view displays links to the following actions on my projects controller:

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 or manually change the record on the database. Voting works as expected, with up_votes and down_votes being stores in the correspondent project record.

However if I run the cron job I setup to take care of this, which is the normal application workflow, the views render the voting links, the users can click them as usual, but no vote is stored on the projects table. The votings table, however, gets populated normally and the user is not able to vote again.

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 a last observation: I ran a migration to add the voting_deadline column to the projects 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.

I really appreciate any input.

Was it helpful?

Solution

Turns out it was just a validation error that was not being raised by the gem. But it was my fault, completely, since the validation should occur only on create, and it was happening on updates too.

However, I think the code for the gem could improve, pointing out errors like such, so I'll provide some feedback at their github page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top