Question

I'm building an application where a user can build a lesson from an assortment of standards and questions to teach the standards, but I'm not exactly sure if I have set up everything correctly or not.

The 'new' page allows the user to use drop down menus to sort through to select the standards through the Lesson Controller

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

Once the standards are selected, the user is redirected to the 'edit' page which shows each of the selected standards, but this is the part where I'm having trouble with and I'm not sure my models are set up correctly. There is a has_many through relationship between lessons and standards to select standards, and also a has_many through relationship between lessons and questions as well to select the questions associated with each standard.

I'm trying to list each of the questions associated with the standards underneath the parent standard, I have tried @questions = @standards.questions in the 'edit' method, but an ActiveRecord Relation NoMethod error is called. I have also tried @questions = Question.where(:standard_id => @standards) in the controller, but the page lists all of the questions for all of the selected standards underneath each standard.

My lesson model:

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

Standard model:

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

Question model:

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
Was it helpful?

Solution

The problem seems to be related to Rails not being able to figure out proper class names for your association names. For example, you've specified has_many :select_standards for your class Selection, by default Rails would search for SelectStandard class for association name :select_standards. The fix is easy in this case, which is to either change the association declaration to:

has_many :selections

or add class_name to association as:

has_many :select_standards, class_name: 'Selection'

You need to make sure that this is done for any and all the custom association names that do not match the actual ActiveRecord inherited class names.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top