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 8790297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:36:52+00:00 2026-06-13T22:36:52+00:00

I have a Subject Model and a Lesson Model. I implemented a nested model

  • 0

I have a Subject Model and a Lesson Model.
I implemented a nested model form.

After subject creation, I led it to a page where it supposedly shows the lessons associated. However, I fail to see the lessons.

I believe the data for lessons did not get saved properly as
When I did a for example lesson.find_by_subject_id(‘1’), I get ‘nil’ in return.

I am trying to figure out how polymorphism works on rails and would appreciate it if someone could either point out where I’ve gone wrong or give me some guidance on how those to pass the values for belong_to classes to be created.

Subject Model

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

Lesson Model

attr_accessible :lessonName, :subject, :subject_id
belongs_to :subject

Subject Controller

def new
3.times {@subject.lessons.build}
end

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

Form

<%= form_for @subject do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :subjectCode %><br />
    <%= f.text_field :subjectCode %>
  </p>

  <%= f.fields_for :lessons do |builder| %>
    <p>    
    <%= builder.label :lessonName %> <br/>
    <%= builder.text_area :lessonName, :rows=>3 %>
    </p>
  <% end %>
  <p><%= f.submit "Submit" %></p>
<% end %>

Routes
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-13T22:36:53+00:00Added an answer on June 13, 2026 at 10:36 pm

    Your reject_if lambda will always reject the lessons attributes because lessons don’t have a content attribute, so you’re essentially evaluating nil.blank? which will return true

    Perhaps you want to check if the lesson name is blank? Ala :reject_if => lambda { |a| a[:lessonName].blank? }

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

Sidebar

Related Questions

I have a model Subject and a nested Model Lessons I am encountering the
I have a simple contact form with Subject and Message that I want to
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 model Data , associated to a table like this (The model
In a model say Task , I have following validation validates_presence_of :subject, :project, :user,
I have two models as subject and teacher Subject model as class Subject <
I have two models, subject and page . I created a one-to-many association between
I have a model for an ASP.NET MVC view containing several properties: Subject Message

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.