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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:45:16+00:00 2026-05-19T11:45:16+00:00

I have a model: class Contact < ActiveRecord::Base has_many :phones accepts_nested_attributes_for :phones end I

  • 0

I have a model:

class Contact < ActiveRecord::Base
  has_many :phones
  accepts_nested_attributes_for :phones
end

I want to build 50 phone #s that users can add (there may already be phones 1 or 5, but I always want 50 available)
In my controller:

while contact.phones.length < 50
  contact.phones.build({:phone_type_id => PhoneType['a_cool_type'].id})
end

In my view, I want to have 2 columns of phone #s 25 rows each

 <%= semantic_form_for contact do |form| %>
   <table width=50%>
   <%= form.inputs :for => :phones[0..25] do |phone_form| %>            
      <td align="center"><%= phone_form.input :number, :label => false %></td>
       ....
   <% end %>
   </table>             
   <table width=50%>
   <%= form.inputs :for => :phones[25..49] do |phone_form| %>           
      <td align="center"><%= phone_form.input :number, :label => false %></td>
       ....
   <% end %>
   </table>
<%end %>

Obviously the line:

<%= form.inputs :for => :phones[25..49] do |phone_form| %>

doesn’t work, but it conveys my intention ( I hope). I want to have more control over how formtastic grabs the underlying object association.

The following works, but I can’t do two columns easily without fancy css.

<%= form.inputs :for => :phones do |phone_form| %>

Any suggestions?

———- Update —-

I was able to get around this in a roundabout way:
I built up a separate list of phone #s not as contact.phones.build, but Phone.new(:contact_id => contact.id) and store those in a list called @new_phones

Then my form looks like this:

<%= semantic_form_for @contact, :url => ..., do |f| %>
   <% @new_phones[0...25].each_with_index do |phone, i| %>
      <%= f.fields_for :phones, phone, :child_index => i do |phone_form| %>             
         <%= render "phone_fields", {:phone_form => phone_form, :phone => phone} %>
      <%end%>
    <% end %>
   ....
   <% @new_phones[25...50].each_with_index do |phone, i| %>
        <%= f.fields_for :phones, phone, :child_index => i+25 do |phone_form| %>                
        <%= render "phone_fields", {:phone_form => phone_form, :phone => phone} %>
    <%end%>
  <% end %>  
<%end%>

This allowed me to display 25 phones on one part of the page, and 25 on another, with nested_attributes_for :phones working as expected on form submit.

  • 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-19T11:45:17+00:00Added an answer on May 19, 2026 at 11:45 am

    I’ve always had problems with getting nested attributes working as I want but this may help resolve your issue.

    Model:

    class Contact < ActiveRecord::Base
      has_many :phones
      accepts_nested_attributes_for :phones
    end
    

    Controller:
    See we’re looping @contract.phones.build 50 times, this creates 50 new instances.

    class Contact < ApplicationController
      def new
        @contact = Contact.new
        25.times do
          @contact.phones.build
        end
    
      end
    end
    

    View new.html.erb :

    ...
    <%= p.semantic_fields_for :phones do |ec| %>
      <%= ec.input :number %>
    <% end %>
    ...
    

    I did try a few attempts to intercept the loop, sadly with no definite clean avail.

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

Sidebar

Related Questions

I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end
I have the following models set up: class Contact < ActiveRecord::Base belongs_to :band belongs_to
I have the following two models class ContactField < ActiveRecord::Base end class Address <
I have a model that has a unique generic foreign key relationship: class Contact(models.Model):
I have a following model: class Article < ActiveRecord::Base define_index do indexes content indexes
Given the following models: class Room < ActiveRecord::Base has_many :contracts end class Contracts <
It seems simple, in my model I have: class CustomerAccount < ActiveRecord::Base acts_as_url :name
I have a model called Contact which has_one guest like so. class Contact <
I have one grails application.In that I have one model class named Book. From
I have an Address Model which has a ForeignKey to a Contact Model: class

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.