2014-01-15 3 views
0

머리말 :이 질문은 매우 구체적입니다. 나는 이걸두고 내 머리를 짚어 봤는데 ... 나는이 사람을위한 전문가가 필요하다고 생각해.레일 4 - 복잡한 모델 관계

개념 : : "시험 응시자"는 객관식 질문에 답해야합니다 (보통 정답> 1). 사용자가 올바른 대답 (그림)을 선택한 다음 "제출"을 클릭하십시오.

셈 이죠 이런 식으로 :

Which ones need baking?

내 문제 :수집에 내가 가장 좋은 방법을 모르고 (를 검색 결국 다음과)를 저장의 데이터 이런 종류의 일. 구체적으로 :

  • 어떤 사용자가이 질문에 답하려고 했습니까?
  • 사용자가 선택한 사진은 무엇입니까?
  • 어떤 사용자 선택 사진이 옳았거나 틀렸습니까?
  • 이 질문이 전체적으로 정확 했습니까? (예를 들어, 사용자는 부분적으로 만 옳다는?)

내 테이블 구조 :

users 
    id 
=================================================== 
templates 
    id 
    prompt     #"Which one needs baking?" 
=================================================== 
choices     #e.g., the image 
    id 
    name 
    image 
=================================================== 
template_assignments #a join table between templates and choices 
    id 
    choice_id 
    template_id 
    correct    #boolean - Is this image the correct response? 
=================================================== 
responses    #this is where I am LOST 
    id 
    user_id 
    template_id 
    --Not sure what to do with this table, maybe something like: 
    response_1 
    response_1_correct #boolean 
    etc.... 
    overall_correct  #boolean 
    -- Or would I need some other type of join table? 

나의 관계 :

class Choice < ActiveRecord::Base 
    has_many :template_assignments 
    has_many :templates, :through => :template_assignments 
    end 

    class Template < ActiveRecord:Base 
    has_many :template_assignments, dependent: :destroy 
    has_many :choices, :through => :template_assignments 
    has_many :responses 
    accepts_nested_attributes_for :template_assignments, allow_destroy: true 
    end 

    class TemplateAssignment < ActiveRecord:Base 
    belongs_to :template 
    belongs_to :choice 
    end 

    class Response < ActiveRecord::Base 
    belongs_to :template 
    end 

방법에 대한 제안 이 모델들 사이의 관계를 정의한다. 매우 도움이된다!

감사합니다.

답변

0

이것은 프로젝트를 설정하는 방법입니다. 당신은 당신이 선택을 저장하려면 몇 테이블

Templates  
* id 
* prompt 

Choices  
* id 
* name 
* image 

Responses 
* template_id 
* user_id 
* score 

TemplateChoices 
(there should be 4 rows for the question above) 
* template_id 
* choice_id 

TemplateSolutions 
(if there are three correct answers, then there should only be 3 rows) 
* template_id 
* choice_id 

가입 필요로하는 사용자는 당신이이 질문에 대한 도움이 게시물을 업데이트 할 수 있습니다
ChoiceResponses 
* choice_id 
* response_id 

했다. (BTW에서는 '레일스 테이블 명명 규칙을 가능한 많이 조인'을 사용합니다)