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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:55:10+00:00 2026-06-07T10:55:10+00:00

I have several models in nested attributes that I’m working with. I have teams

  • 0

I have several models in nested attributes that I’m working with.

I have “teams” (has many constests), and “contests” (belongs to Team). But I also want contests to reference “categories” as a child object (a contest can only have one category, and a category can have may contests).

The way the logic works is that a team is created first, then a contest, and after that I want to be able to select from a list of categories (in a partial) and establish the association (set the category_id in contest to the id value in a category). It makes sense to me how this is done when creating a new contest as a child of team, but I am hitting my head agains the wall when it comes to creating the second relationship (existing contest to an existing parent category).

The controller that gives me the show view for a contest is:

def show
@team = Team.find(params[:team_id])
@contest = Contest.find(params[:id])
@categories = Category.all

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

end

In the show view I have this code:

<p><b>Name:</b><%= @contest.name %></p>
<%= link_to 'Edit', edit_team_contest_path(@team, @contest) %> |
<%= link_to 'Back', team_contests_path %>
<br />
<%= render 'categories/index'%>

And my partial _index for categories contains this code:

<table>
<% @categories.each do |category| %>
<tr>
<td><%= category.level1 %></td>
<td><%= category.level2 %></td>
<td><%= category.level3 %></td>
<td><%= category.level4 %></td>    
<td><%= link_to 'Show', category %></td>
<td><%= link_to 'Edit', edit_category_path(category) %></td>
<td><%= link_to 'Destroy', category, confirm: 'Are you sure?', method: :delete %></td>
<%end%>
</table>

Where I am so flummoxed is where to place the code (in the Contest or Category controller?) for setting the category-contest parent-child relationship, as well as which view (the Contest show view, or the Category _index partial?). I am pretty certain that I am not understanding something fundamental about Rails here, so if anyone could point me to the docs that might clear up my befuddlement I’d very much appreciate it.

  • 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-07T10:55:12+00:00Added an answer on June 7, 2026 at 10:55 am

    Okay, here’s how I ended up solving my problem (in case anyone finds it later and uses the same search terms I tried):

    Models:

    team.rb
     has_many :contests, :dependent => :destroy
    
    category.rb
     has_many :contests
    
    contest.rb
     belongs_to :team, :foreign_key => "team_id"
     belongs_to  :category, :class_name => 'Category', :foreign_key =>"category_id"
     accepts_nested_attributes_for :category
    

    Controller:

    contests_controller
    def update
    @contest = Contest.find(params[:id])
    @team = @contest.team
    if !params[:category_id].nil?
      @category = Category.find(params[:category_id]) 
      @contest.update_attributes(:category_id => @category.id)
    end
    respond_to do |format|
      if @contest.update_attributes(params[:contest])
       blah
      else
        blah
      end
    end
    end
    

    Categories View (_index), a partial in the contests/show view, includes these three bits of code:

    <table>
    <% @categories.each do |category| %>
    <tr>
    <td><%= form_for [category, @contest] do |f| %>
        <% f.submit "Select" %>
        <% end %></td>
    </tr>
    <%end%>
    </table>
    

    And that is what it takes to associate a record that belong to another parent with another parent in a different model (after the first relationship has been created).

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

Sidebar

Related Questions

I have several models that includes nested classes and lists. Many of the class
I have several Models: Workspace User Asset WorkspaceUser WorkspaceAsset Workspace has many users and
I have an existing Django project that has several models using concrete inheritance of
I have several models that need to call a method that takes a particular
I have several models that belong_to :status . Statuses are: Active Hidden Banned I
I have several Mongoid models that I'm running mapreduce on, and I'd like to
If you have several view models on one page, how do you ensure that
I have several small classes that are in a single file in /app/models, similar
What's the rails way to see posts nested beneath several associative models that a
I have several models indexed with elasticsearch and one of them has an association

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.