1

표준을 가르치기 위해 다양한 표준 및 질문에서 사용자가 수업을 만들 수있는 응용 프로그램을 구축하고 있지만 올바르게 설정했는지 정확하게 알 수는 없습니다. 아닙니다.여러 개의 레일 has_many : 연결을 통해 표시 및 연결

'새'페이지는 사용자가 표준이 선택되면, 사용자가 '편집'로 재 레슨 컨트롤러

def new 
@search = Standard.search(params[:q]) 
@standards = @search.result 
@lesson = Lesson.new 
end 

def create 
@lesson = current_user.selects.build(params[:lesson]) 

    if @lesson.save 
    redirect_to edit_lesson_path(@lesson) 
    else 
    render :action => 'new' 
end 
end 

def edit 
@lesson = Lesson.find(params[:id]) 
@standards = @lesson.standards 
end 

을 통해 표준을 선택을 통해 정렬 드롭 다운 메뉴를 사용할 수 있습니다 각 표준을 보여주는 페이지, 그러나 이것은 내가 문제가있는 부분이고 내 모델이 올바르게 설정되었는지 확신 할 수 없습니다. 표준을 선택하는 수업과 표준 사이에는 has_many through 관계가 있으며, 각 표준과 관련된 질문을 선택하기 위해 수업과 질문 사이에 has_many through 관계가 있습니다.

상위 표준 아래의 표준과 관련된 각 질문을 나열하려고하는데 '편집'메서드에서 @questions = @ standards.questions를 시도했지만 ActiveRecord 관계 NoMethod 오류가 호출되었습니다. 또한 @questions = Question.where (: standard_id => @standards)를 컨트롤러에서 시도했지만 페이지에는 각 표준의 모든 선택된 표준에 대한 모든 질문이 나열되어 있습니다.

내 수업 모델 :

class Lesson < ActiveRecord::Base 
attr_accessible :user_id, :name, :grade_id, :text_id, :date, :subject_id, :question_ids 

has_many :select_standards 
has_many :standards, through: :select_standards 

has_many :select_questions 
has_many :questions, through: :select_questions 
end 

표준 모델 :

class Standard < ActiveRecord::Base 
attr_accessible :content, :grade_id, :subject_id 
belongs_to :subject 
belongs_to :grade 
has_many :questions 
end 

질문 모델 :

class Question < ActiveRecord::Base 
attr_accessible :content, :standard_id 
belongs_to :standard 
has_many :select_questions 
has_many :lessons, through: :select_questions 
end 

Select_standards :

class Selection < ActiveRecord::Base 
attr_accessible :lesson_id, :standard_id 
belongs_to :lesson 
belongs_to :standard 
end 
+0

'select_standards'란 무엇입니까? 모델 정의를 게시 할 수도 있습니까? – vee

+0

Select_standards는 사용자 선택을 보관하는 수업과 표준 사이의 조인 모델입니다. 그러나 나는 내 문제를 해결했다고 생각한다! 내 질문 모델에 "delegate : lesson, : to => : standard, : allow_nil => true"를 추가 했으므로 이제는 @ lesson.questions를 호출 할 수 있습니다. 도와 줘서 고마워. :) –

답변

0

문제는 레일스가 사용자의 연결 이름에 적합한 클래스 이름을 파악할 수 없다는 것과 관련이있는 것으로 보입니다. 예를 들어, 클래스 Selection에 대해 has_many :select_standards을 지정했으면 기본적으로 레일스는 연관 이름 :select_standards에 대해 SelectStandard 클래스를 검색합니다.

has_many :selections 

또는 협회에 class_name을 추가합니다 : 당신이 어떤 수행됩니다 있는지 확인해야

has_many :select_standards, class_name: 'Selection' 

수정 프로그램에 연관 선언을 변경하거나이 경우에 쉽게 실제 ActiveRecord 클래스 이름과 일치하지 않는 모든 사용자 지정 연결 이름이 있습니다.

+0

그것은 내 실수로 간단한 실수 였지만 완벽하게 잘 작동했습니다! 도와 주셔서 정말 감사합니다! –