default for database migration must be set during the migration start - adding a default value after creating the table will not work.
If your database has already been carved (and you do not want to change the scheme), the following capture in ActiveRecord will perform the task:
class Answer < ActiveRecord::Base attr_accessible :is_correct, :question_id, :title, :sms_answer_code, :votes belongs_to :question before_save :default_vote_count def upvote self.votes += 1 end def default_vote_count self.votes ||= 0 end end
EDIT:
If you want to change the actual default value in the database, you can create a migration of changes containing the following:
# in console rails g migration change_default_for_answer_votes
Some databases (such as Postgres) do not automatically assign new updated default values ββto existing column entries, so you need to iterate over existing answers to manually update each one before the default vote count:
# in console Answer.update_all(votes: 0)
source share