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

  • Home
  • SEARCH
  • 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 8765559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:18:48+00:00 2026-06-13T16:18:48+00:00

I have a model Subject and a nested Model Lessons I am encountering the

  • 0

I have a model Subject and a nested Model Lessons

I am encountering the problem of unknown attribute: subject_id

I have tried the different solutions suggested at Rails 3 Nested Models unknown attribute Error but I am still receiving the error.

I am pretty new to Rails and I can’t seem to figure out what went wrong. I would appreciate it if someone could help me out.

Here are my relevant files.

Subject Model

  attr_accessible :subjectCode, :subject_id

  has_many :lessons, :dependent => :destroy
  accepts_nested_attributes_for :lessons, :reject_if => lambda { |a| a[:content].blank?     }, :allow_destroy => true

end

Lesson Model

  attr_accessible :lessonName
  belongs_to :subject

Subject Controller

def show
  @subject = Subject.find(params[:id])
end

def new
  @subject = Subject.new
  @lesson = @subject.lessons.build
end

def create
  @subject = Subject.new(params[:subjectCode])
  if @subject.save
    redirect_to @subject, :notice => "Successfully created subject."
  else
    render :action => 'new'
  end
end

Subject Form

<%= form_for @subject do |f| %>
<%= f.error_messages %>
<p>
  <%= f.label :subjectCode %><br />
  <%= f.text_field :subjectCode %>
</p>
<%= f.fields_for :lessons, @lesson do |builder| %>
<p>    
<%= builder.label :lessonName %> <br/>
<%= builder.text_area :lessonName, :rows=>3 %>
</p>
<% end %>
<p><%= f.submit "Submit" %></p>

routes.rb

resources :subjects do resources :lessons 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-13T16:18:50+00:00Added an answer on June 13, 2026 at 4:18 pm

    The subject_id should be put in Lesson model, because it belongs to Subject, you should put it in accessible too, and also puts lessons_attributes in attr_accessible in Subject model. I think the code to build Subject in your create action should be:

    @subject = Subject.new(params[:subject]), not

    @subject = Subject.new(params[:subjectCode])

    Try this and see something happens.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my Activity model, I have a default scope: default_scope where(:subject_id => Log.get_subject_id) Problem
I have a Subject Model and a Lesson Model. I implemented a nested model
I have a subject model with two fields - name and level. I want
I have a model that looks like this: [DisplayName(Subject)] public string SubjectID { get;
I have the following in my model: public string Subject { get { return
I have a very odd problem with unobtrusive validation. I have a model with
In a model say Task , I have following validation validates_presence_of :subject, :project, :user,
hi have a model Invoice on which i have a subject_id field referencing a
I have a model for an ASP.NET MVC view containing several properties: Subject Message
I have this domain model, grails-app/domain , named com.portal.Schedule.groovy having this properties: Subject subject

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.