Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refine answer model #27

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions app/models/answer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Answer < ActiveRecord::Base
belongs_to :quiz

validates :answer, presence: true, length: { in: 1..30 }
validates :right_or_wrong, presence: true
validates :quiz_id, presence: true
validate :right_answer_is_only_one_by_one_quiz

def right_answer_is_only_one_by_one_quiz
if quiz.answers.where(right_or_wrong: true) > 1
error.add(:right_answer_number, "1つの問題に対して正解は1つだけです")
end
end
end
11 changes: 11 additions & 0 deletions db/migrate/20160317000000_create_answers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateAnswers < ActiveRecord::Migration
def change
create_table :answers do |t|
t.string :answer_text, :null => false
t.boolean :right_or_wrong, :null => false
t.belongs_to :quiz, index: true, foreign_key: true

t.timestamps null: false
end
end
end
7 changes: 7 additions & 0 deletions spec/factories/answers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryGirl.define do
factory :answer do
answer_text "MyString"
right_or_wrong false
quiz_id nil
end
end
5 changes: 5 additions & 0 deletions spec/models/answer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Answer, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end