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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:02:18+00:00 2026-06-09T13:02:18+00:00

In my Contract form, I allow the user to select a Unit from a

  • 0

In my Contract form, I allow the user to select a Unit from a dropdown box, OR select multiple Units from a group of check boxes. The dropdown field is named unit_id and the multiple checkboxes are named multi_unit_ids[]. (since both of these options are on the page, I cannot use the same name).

There is 1 contract created per unit chosen. So if only 1 unit is chosen, then only a single Contract is created with that unit_id. However, when choosing multiple Units, all of the data is the same for each Contract created, however each has their own Unit ID (pulled from the multi_unit_ids array).

Here is the code in my create method of my contracts_controller.rb:

# Multiple Units? Multiple Contracts
if params[:multi_unit_id]
  unit_arr = params[:multi_unit_id]
else
  unit_arr = [*params[:contract][:unit_id]]
end


# Loop through units
unit_arr.each do |unit_id|
  # Assign the unit id to the params for easy creation
  params[:contract][:unit_id] = unit_id

  @contract = Contract.new(params[:contract])
  # ... other code here
  @contract.save
end

This all seems so messy! What is a better way to do this?

  • 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-09T13:02:19+00:00Added an answer on June 9, 2026 at 1:02 pm

    Well I can’t make much of a suggestion as far as creating these database entries without a loop goes. I’m not sure whether that’s possible, and really that loop doesn’t seem messy enough to really stress out about, unless there’s a mess in the # ... other code here section that you’ve omitted.

    In fact, what I’m about to suggest might make you feel the code’s more messy.

    If you plan on creating a lot of database rows, it would probably be a good idea to wrap the loop in an ActiveRecord::Base.transaction similar to:

    # Loop through units
    ActiveRecord::Base.transaction do
      unit_arr.each do |unit_id|
        # Assign the unit id to the params for easy creation
        params[:contract][:unit_id] = unit_id
    
        @contract = Contract.new(params[:contract])
        # ... other code here
        @contract.save
      end
    end
    

    Or, otherwise use some other means of combining the creation into a single query (there are other options, and benchmarks available at http://www.coffeepowered.net/2009/01/23/mass-inserting-data-in-rails-without-killing-your-performance/).

    As far as the refactoring goes, though, I can’t offer much here.

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

Sidebar

Related Questions

I have a form field where a user enters contact information, including name, email,
I'm building a contact form that appears in a jQuery popup box. When you
I have an online user roster populated from a database. Each username is a
So how can I pass a value from one form to another? For example:
I am working on a wizard form framework that will allow me easily create
I'm using Django Contact Form on a website to allow visitors to send emails.
I have a search form and result list. The form allows the user to
I want to use AutoCompleteExtender with textbox to auto complete user's entry from mysql
I'm trying to set up a form where the user can click on a
How can I allow user input on a page to change the HTML or

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.