因此,我尝试将“投票”列的默认值设置为 0,但是当我在 rails c 或通过单元测试创建答案实例时,投票值始终为nil
. 关于为什么这不起作用的任何想法?
我已经像这样更改了迁移:
class AddVotesToAnswers < ActiveRecord::Migration
def change
add_column :answers, :votes, :integer, default: 0
end
end
这是模型:
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
测试规范
需要'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