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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:59:36+00:00 2026-05-29T15:59:36+00:00

I have a Section model, where a Section can be a parent of another

  • 0

I have a Section model, where a Section can be a parent of another Section (subsection).

Here is my model:

class Section < ActiveRecord::Base
  has_many :exercises

  has_one :parent_link,
    :foreign_key => 'subsection_id',
    :class_name => 'SectionLink',
    :dependent => :destroy

  has_one :parent, :through => :parent_link

  has_many :subsection_links,
    :foreign_key => 'parent_id',
    :class_name => 'SectionLink',
    :dependent => :destroy

  has_many :subsections, :through => :subsection_links

  attr_accessor :parent_id

  def to_param
    "#{id}-#{description.parameterize}"
  end

  def self.search(search)
    if search
      find(:all, :conditions => ['description LIKE ?', "%#{search}%"])
    else
      find(:all)
    end
  end
end

And the association model:

class SectionLink < ActiveRecord::Base
  belongs_to :parent, :class_name => 'Section'
  belongs_to :subsection, :class_name => 'Section'
end

My controller:

class SectionsController < ApplicationController
  # GET /sections
  # GET /sections.json
  def index
    @sections = Section.order("subsections_count DESC").search(params[:search])

    respond_to do |format|
      format.html # index.html.erb
    end
  end

  # GET /sections/1
  # GET /sections/1.json
  def show
    @section = Section.find(params[:id])
    @subsections = @section.subsections
    @exercises = @section.exercises

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @section }
    end
  end

  # GET /sections/new
  # GET /sections/new.json
  def new
    @section = Section.new
    @section.parent_id = params[:parent]

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

  # GET /sections/1/edit
  def edit
    @section = Section.find(params[:id])
  end

  # POST /sections
  # POST /sections.json
  def create
    @section = Section.new(params[:section])
    @parent = @section.build_parent(:parent_id => @section.parent_id) unless @section.parent_id.empty?

    respond_to do |format|
      if @section.save
        format.html { redirect_to @section, notice: 'Section was successfully created.' }
        format.json { render json: @section, status: :created, location: @section }
      else
        format.html { render action: "new" }
        format.json { render json: @section.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /sections/1
  # PUT /sections/1.json
  def update
    @section = Section.find(params[:id])

    respond_to do |format|
      if @section.update_attributes(params[:section])
        format.html { redirect_to @section, notice: 'Section was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @section.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /sections/1
  # DELETE /sections/1.json
  def destroy
    @section = Section.find(params[:id])
    @section.destroy

    respond_to do |format|
      format.html { redirect_to sections_url }
      format.json { head :no_content }
    end
  end
end

The parent ID is fed in via a hidden field in the form:

<%= form_for(@section) do |f| %>
  <% if @section.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@section.errors.count, "error") %> prohibited this section from being saved:</h2>

      <ul>
      <% @section.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :description %><br />
    <%= f.text_field :description %>
  </div>
  <div class="field">
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </div>
  <%= f.hidden_field :parent_id, :value => @section.parent_id %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

I’m getting

undefined method 'build_parent' for #<Section:0xb4f1764c>

Is there a better way of modelling this association? Why is build_parent undefined?

UPDATE:

Now works with following controller code:

@section = Section.new(params[:section])
unless @section.parent_id.empty?
  @parent = Section.find(@section.parent_id)
  @section.parent = @parent
end

Looking for any suggestions on how it can be improved, and why it did not work before…

  • 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-05-29T15:59:39+00:00Added an answer on May 29, 2026 at 3:59 pm

    You’re looking to do a self join.

    class Section < ActiveRecord::Base
      has_many :subsections, :class_name => "Section"
      belongs_to :parent_section, :class_name => "Section",
        :foreign_key => "parent_id"
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of model classes from another section of the application that
I have following models relation: class Section(models.Model): section = models.CharField(max_length=200, unique=True) name = models.CharField(max_length=200,
I have a section of my site where users can add addresses to their
I have a section of code that can be summarised as follows; void MyFunc()
I have a section on a page where a user can change their username.
I have a Model called Section which has many articles ( Article ). These
I have FileField in my django model: file = models.FileField(upload_to=FOLDER_FILES_PATH) In Django admin section
I have a couple of Django models set up like this: class Group(models.model): name
I have a list of articles, and each article belongs to a section. class
I have the following model: [DataContract] public class MessageHeader { private Guid? messageId; public

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.