ユーザーがさまざまな標準と質問からレッスンを構築して標準を教えることができるアプリケーションを構築していますが、すべてを正しく設定したかどうかは正確にはわかりません。
「新しい」ページでは、ユーザーはドロップダウン メニューを使用してソートし、レッスン コントローラーを介して基準を選択できます。
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 スルー関係と、各基準に関連付けられた質問を選択するためのレッスンと質問の間の has_many スルー関係もあります。
親標準の下にある標準に関連付けられている各質問をリストしようとしています。「編集」メソッドで @questions = @standards.questions を試しましたが、ActiveRecord Relation 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