1

ユーザーがさまざまな標準と質問からレッスンを構築して標準を教えることができるアプリケーションを構築していますが、すべてを正しく設定したかどうかは正確にはわかりません。

「新しい」ページでは、ユーザーはドロップダウン メニューを使用してソートし、レッスン コントローラーを介して基準を選択できます。

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
4

1 に答える 1

0

この問題は、Rails が関連付け名の適切なクラス名を見つけられないことに関連しているようです。たとえば、has_many :select_standardsclassを指定したとします。デフォルトでは、Rails は関連付け名のクラスをSelection検索します。この場合、関連付け宣言を次のように変更することで、修正は簡単です。SelectStandard:select_standards

has_many :selections

class_nameまたは次のように関連付けに追加します。

has_many :select_standards, class_name: 'Selection'

ActiveRecord実際の継承されたクラス名 と一致しないすべてのカスタム アソシエーション名に対して、これが行われることを確認する必要があります。

于 2013-08-18T23:39:08.090 に答える