Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9151283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:45:58+00:00 2026-06-17T11:45:58+00:00

I currently have two models School and Course where School has_many courses, and Course

  • 0

I currently have two models School and Course where School has_many courses, and Course belongs_to school. Additionally, School and Course are nested resources, where School is the parent resource, and Course the child.

I have created several test records in the Rails Console so that a query such as when the child calls upon the parent Course.first.school successfully executes and returns all the relevant information of the school Course.first is associated with.

However, when put into a controller function, I would instead get an error “undefined method `school’ for nil:NilClass” for the following line:

redirect_to school_course_path(@course.school, @course)

.. as if the .school part wasn’t recognized (where as it was in the console). Why is this the case, and how do I get past this error? Thanks!

Edit – as suggested, it could be that my @course instance variable isn’t passed from method to method in the controller. I have attempted at passing them through via a private method, but its still giving me the same error. Here is my code (background: the model Question belongs_to Course, with Course having many questions. Course isn’t part of the nested routes)

class QuestionsController < ApplicationController

  def new
    @course = Course.find(params[:course]) #confirmed working
    self.current_course = @course #I attempt to set current_course, a private method 
    @question = Question.new
  end

  def create
    @question = Question.new(params[:question]) #also works, in rails console all the questions confirms to have rails id
    if @question.save
      redirect_to school_course_path(current_course.school, current_course) #source of my frustrations - continues to returns same error message
    else
      render 'new'
    end
  end

  private 

  def current_course=(course)
    @current_school = course
  end


  def current_course
    @current_course
  end

end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:45:59+00:00Added an answer on June 17, 2026 at 11:45 am

    Should work if your relationships are set up the way I think they are:

    def create
      @question = Question.new(params[:question])
      @course = @question.course
      if @question.save
        redirect_to school_course_path(@course.school, @course)
      else
        render 'new'
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have two different models: User and Project . The User model has
I currently have two Backbone models: user and project. I would like to have
I currently have two models: Product and Service. Both share the same table structure
I currently have two models: Campaigns and Videos. Videos belongs to Campaigns and a
I have two models, users and themes, that I'm currently joining in a HABTM
I currently have a Rails 3.0.10 app and I made two models with simple
Currently i have two models: Rate and Item Rate is a model for voting,
My question is the following: I have two models : Products & brands. Currently
Hi I'm currently going through a tutorial on unit tests. I have two models:
I have two ActiveRecord models, A and B . A has_many :B and B

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.