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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:40:35+00:00 2026-05-12T23:40:35+00:00

Given these relationships: class Account < ActiveRecord::Base has_many :employments has_many :people, :through => :employments

  • 0

Given these relationships:

class Account < ActiveRecord::Base
  has_many :employments
  has_many :people, :through => :employments
  accepts_nested_attributes_for :employments
end

class Employment < ActiveRecord::Base
  belongs_to :account
  belongs_To :person
end

I’m trying to list the employment records for an account:

<% form_for @account do |f| -%>
  <% f.fields_for :employments do |e| -%>
    <%= render :partial => 'employment', :collection => @account.employments, :locals => { :f => e } %>
  <% end -%>
<% end -%>  

I’ve verified that the employment table in @account contains two records, but I get four copies of the partial because it iterates employments twice:

Employment Load (1.0ms)   SELECT * FROM [employments] WHERE ([employments].account_id = 1) 
Person Load (1.3ms)   SELECT * FROM [people] WHERE ([people].[id] = 2) 
Rendered accounts/_employment (17.9ms)
Person Load (1.5ms)   SELECT * FROM [people] WHERE ([people].[id] = 1) 
Rendered accounts/_employment (5.1ms)
Rendered accounts/_employment (2.2ms)
Rendered accounts/_employment (2.1ms)

Can anybody explain why that would happen?


Here’s some additional information:

The _employment.html.erb partial:

<div class="employment">
  <span class="name"><%= link_to h(employment.person.name), person_path(employment.person) %></span>
  <span class="role"><%=h employment.role %></span>
  <span class="commands"><%= remove_child_link "Remove", f %></span>
</div>

remove_child_link is the only place I need to generate a form field at. It creates the _delete field for the record and wires up a remove link that changes the value to ‘1’. The ‘role’ property may also be editable, though. The important thing is I don’t want all of the fields to be editable.

The accounts_controller actions for this view:

def edit
  @account = Account.find(params[:id])
end

def update
  @account = Account.find(params[:id])

  respond_to do |format|
    if @account.update_attributes(params[:account])
      flash[:notice] = "#{@account.business_name} was successfully updated."
      format.html { redirect_to @account }
    else
      format.html { render :action => "edit" }
    end
  end
end

Ben got me going in the right direction. Some runtime inspection reveals that the record is stored in the object variable (which I already knew, but in a different context). So I can rewrite the fields_for clause as:

<% form_for @account do |f| -%>
  <% f.fields_for :employments do |e| -%>
    <div class="employment">
      <span class="name"><%= link_to h(e.object.person.name), person_path(e.object.person) %></span>
      <span class="role"><%=h e.object.role %></span>
      <span class="commands"><%= remove_child_link "Remove", e %></span>
    </div>
  <% end -%>
<% 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-05-12T23:40:36+00:00Added an answer on May 12, 2026 at 11:40 pm

    You are correct to use fields_for, but it will render what’s inside of it for each employment, so remove the :collection parameter from render :partial. Instead, use nested forms by putting this in your Account model:

    accepts_nested_attributes_for :employments
    

    read more about nested forms here: http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes

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

Sidebar

Related Questions

Assume a standard has_many :through relationship among three models class Person < ActiveRecord::Base has_many
This is easier to explain with an example. Given these two classes: public class
Given these two queries: Select t1.id, t2.companyName from table1 t1 INNER JOIN table2 t2
Given these tables: create table Orders ( Id INT IDENTITY NOT NULL, primary key
I am learning Java and have been given these options: How can you implement
i am using asynctask to fetch images from given url.these images are displaying in
can any one explain inter relation between these below given directives ; Do not
Given file names like these: /the/path/foo.txt bar.txt I hope to get: foo bar Why
Given a BehaviorSubject, what is the practical difference between calling all of these different
Given the Java code below, what's the closest you could represent these two static

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.