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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:53:10+00:00 2026-06-17T13:53:10+00:00

I am working on a Rails application and currently I have 2 models –

  • 0

I am working on a Rails application and currently I have 2 models – Subjects and Lessons.
A Subject has 3 different types of lessons – Lecture, Tutorial and Laboratory. I modelled such that there are 3 has_one to the Lesson model.

Right now, I am trying to create a nested form for subjects and lessons but the lecture, tutorial and laboratory being saved was always the first form that was rendered.
i.e. I have 3 nested forms separately for Lecture, Tutorial and Laboratory but the Lecture, Tutorial and Laboratory that was saved was always the one that was first built. In my codes the lecture was first built so the attributes for tutorial and laboratory would follow the one that I have filled in for my lecture.

I am not sure where I have went wrong or even if having multiple has_one relationship works in this case so any advice would be appreciated.

The related codes are as follows:

The subject model

class Subject < ActiveRecord::Base

  has_one :lecture, :class_name => "Lesson"
  has_one :laboratory,:class_name => "Lesson"
  has_one :tutorial, :class_name => "Lesson"

  accepts_nested_attributes_for :lecture
  accepts_nested_attributes_for :laboratory
  accepts_nested_attributes_for :tutorial 

end

The lesson model

class Lesson < ActiveRecord::Base
  belongs_to :subject
end

The Subject and lesson nested form

<%= form_for(@subject_list) do |f| %>
  <div class="field">
    <%= f.label :subject_code %><br />
    <%= f.text_field :subject_code %>
  </div>
  <div>
    <%= f.fields_for :lecture do |lecture| %>
      <%= render "lecture_fields", :f => lecture %>
    <% end %>
  </div>
  <div>
    <%= f.fields_for :tutorial do |tutorial| %>
      <%= render "tutorial_fields", :f => tutorial %>
    <% end %>
  </div>
  <div>
    <%= f.fields_for :laboratory do |laboratory| %>
      <%= render "laboratory_fields", :f => laboratory %>
    <% end %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

The new action in the subject controller

    def new
    @subject = Subject.new

    lecture = @subject.build_lecture
    laboratory = @subject.build_laboratory
    tutorial = @subject.build_tutorial

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @subject }
    end
  end

I would appreciate if someone could help me out in identifying where I have went wrong. If in the case that I should not be creating such multiple relationships, I would like to have some advice on how could I actually render out 3 forms with a default field indicating the lesson type.

  • 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-17T13:53:11+00:00Added an answer on June 17, 2026 at 1:53 pm

    I’m not really sure if that works, but my advise is to use AR inheritance

    class Lesson < ActiveRecord::Base
    end
    
    class LectureLesson < Lesson
      belongs_to :subject
    end
    
    class LaboratyLesson < Lesson
      belongs_to :subject
    end
    
    class TutorialLesson < Lesson
      belongs_to :subject
    end
    
    class Subject
      has_one :lecture_lesson
      has_one :laboratory_lesson
      has_one :tutorial_lesson
    
      accepts_nested_attributes_for :lecture_lesson
      accepts_nested_attributes_for :laboratory_lesson
      accepts_nested_attributes_for :tutorial_lesson
    end
    

    Migration

    class LessonsAndSubjects < ActiveRecord::Migration
      def up
        remove_column :subjects, :lesson_id
    
        add_column :subjects, :lecture_lesson_id, :integer
        add_column :subjects, :laboratory_lesson_id, :integer
        add_column :subjects, :tutorial_lesson_id, :integer
    
        add_column :lessons, :type, :string
    
        add_index :subjects, :lecture_lesson_id
        add_index :subjects, :laboratory_lesson_id
        add_index :subjects, :tutorial_lesson_id
      end
    
      def down
        remove_column :subjects, :lecture_lesson_id
        remove_column :subjects, :laboratory_lesson_id
        remove_column :subjects, :tutorial_lesson_id
    
        remove_column :lessons, :type
    
        add_column :subjects, :lesson_id, :integer
      end
    end
    

    it makes more sense and it may be fix you issue with nested attributes

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

Sidebar

Related Questions

Background Currently, I am working on a Rails application. I have different products that
I currently have hostingrails.com. But following their tutorial on deploying rails application with FASTCGI
I am currently working on a Rails 3.2 application in which I have two
I'm working on rails application with postgres db.I have a table called merchant_review_votes where
I'm working on a Rails application where I have some a set of two
I am thinking of working on a Rails application that uses PostgreSQL. I have
The Rails application I'm working on, has two central pieces: users and groups. A
We're working on some Ruby on Rails web application which is currently in English
Currently I am working on project like web analytic using rails 3.2.3 I have
I'm working on a Rails application and it is currently hosted on a shared

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.