So I'm trying to set a the default value of a 'votes' column to 0, but when I create instances of answers in rails c or through unit tests, the votes value is always nil. Any ideas on why this isn't working?
I've changed the migration like so:
class AddVotesToAnswers < ActiveRecord::Migration
def change
add_column :answers, :votes, :integer, default: 0
end
end
Here's the model:
class Answer < ActiveRecord::Base
attr_accessible :is_correct, :question_id, :title, :sms_answer_code, :votes
belongs_to :question
def upvote
self.votes += 1
end
end
Test Spec
require 'spec_helper'
describe Answer do
before do
@answer = Answer.make!
end
it "should have a default vote value of zero" do
binding.pry
@answer.votes.should eq(0)
end
end
rake db:test:prepareto get the default value in your test database. 2) you edited the default value in your migration after running it and shouldrake db:migrate:redo