ruby-on-railsrubyruby-on-rails-3updown

Why does the Rails increment method doesn't save the changes?


I have this code to implement a thumbs up/down functionality in my rails app

if params[:vote][:type] == "up"
    answer = Answer.find_by_id(params[:vote][:id])
    answer.increment(:ups)
    render text: answer.ups
end
if params[:vote][:type] == "down"
    answer = Answer.find_by_id(params[:vote][:id])
    answer.increment(:downs)
    render text: answer.downs
end


It doesn't give any error but the incremented value is not updated in the DB.

This code works properly in rails console.

Please help,

Thanks in advance


Solution

  • could you please write the schema of your model, I mean fields with datatype

    another migration with:

    change_column :answers, :ups, :integer, :default: 0
    change_column :answers, :downs, :integer, :default: 0
    

    might help you