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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:09:47+00:00 2026-05-30T22:09:47+00:00

I am new to rails, but not to programming or databases. A BETTER PHRASING

  • 0

I am new to rails, but not to programming or databases.

A BETTER PHRASING OF MY QUESTION IS IN MY ANSWER BELOW.

For simplicity in this example, I have 3 models:
User
Subscription
Default_Subscription

User has_many Subscriptions
Subscription belongs_to User

Default_Subscription has_many Subscriptions
Subscription belongs_to Default_Subscription

Default_Subscription is a pre-populated table with certain types of subscriptions.

During the subscription process, the default subscriptions are listed at one point, and there
is a quantity box alongside each one.

The user enters the quantities for each subscription and hits submit to continue on.

My question is:
How would one go about creating a new subscription for each quantity in a number form?

So you would have a list something like so:

<ol>
   <%= each subscription with quantity box %>
</ol>

<%= button_to %>

When the user hits the button, how do you add up the quantity from each box and add a new subscription for each one? Do I have to use javascript to get the numbers? Do I somehow use rails forms even though this quantities are not associated with any specific database field? Any recommendations or pointing me in the right direction to figure this out on my own would be great.

This form box IS NOT A FIELD FOR ANY MODEL, it’s a count for an association. Let me rephrase: Each quantity in the form boxes represent the number of NEW Subscriptions to be created. Each of these subscriptions BELONGS_TO 1 DEFAULT_SUBSCRIPTION. In essence, the number represents the number of new subscriptions ASSOCIATED WITH THAT DEFAULT SUBSCRIPTION.

I’m using rails 3.2.1, and ruby 1.8.7

Thank you

  • 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-30T22:09:48+00:00Added an answer on May 30, 2026 at 10:09 pm

    I have figured out one way, and reading my original post again, the whole thing is really confusing because I didn’t know how to say exactly what I was trying to accomplish. I must say a lot of the reason I was confused is because the form I wanted did not correspond to any model, just an association count, which is ultimately a basic html form if you want to create a bunch of new objects without having their attributes yet. I’ll first clarify then show my solution.

    Clarification:

    I have 2 Tables:

    1. Subscription
    2. Default_Subscription (Pre-Populated)
    Subscription belongs_to Default_Subscriptions
    Default_Subscription has_many Subscriptions
    

    A User is subscribing to my website. This process is a step by step: not everything happens on the same page.

    This all happens in a subscribe_controller. Each action corresponds to a step in the process.

    One of the actions is default_subscriptions. This action lists the Default_Subscriptions a User can choose from, except they do not just choose, they can enter an amount for each type of Default_Subscription they’d like.

    When the Default_Subscriptions are listed on the default_subscriptions page, I wanted a form with an html number input alongside each of these Default_Subscription. When the form is submitted via a next button, I had no idea how to gather the quantities from each html input and create an array of Subscription.new, with each Subscription’s default_subscription_id corresponding to the proper Default_Subscription.

    One Possible Solution:

    def default_subscriptions
       @def_subscriptions = Default_Subscription.all
    end
    

    Lets say the page I want proceed to after all the quantities are entered on the default_subscriptions page is review_subscriptions.

    Here’s what I did to create the proper form to proceed to the next action in the controller:

    <%= form_tag( {:controller => 'subscribe', :action => 'review_subscriptions'}, :method => 'post' ) do %>
    
    <ol>
        <% @def_subscriptions.each do |ds| %>
            <li>
               <%= ds.name + ' ' %>
               <%= number_field_tag("subscription_counts[#{ds.id}]") %>
            </li>
        <% end %>
    </ol>
    
    <%= submit_tag('Next') %>
    
    <% end %>
    

    The trick here is that string passed to the number_field_tag. By placing a single set of square brackets at the end of the string for a field_tag method parameter, the part before the brackets is the name of the hash, and the thing in the brackets is a key in the hash, and the submit button causes the corresponding value for each key to be the value of the field. Pretty cool!

    The parameters passed to the next action would contain a hash called subscription_counts, and iterating through this hash would give a corresponding new subscription amount for each default_subscription_id. Like so:

    def review_subscriptions
       subscription_counts = params[:subscription_counts]
    
       subscription_counts.each do |id, amount|
    
         counter = Integer(amount)
    
         until counter == 0
           new_subscription = Subscription.new
           new_subscription.default_subscription_id = Integer(id)
    
           @subscriptions << new_subscription  # @subscriptions is an instance variable
    
           counter -= 1
         end
       end
    end
    

    I’d just like to point out, the more I work with them, the more I love them; I love Rails, and I love Ruby. They are super fun and classy. An until loop… how cool is that? If you have other solutions, now that my question is more obvious, please chime in! I know others out there are trying to find some slick ways to create multiple new objects in a one to many association with a single post call like this. Technically my objects aren’t saved in the database yet, but that wouldn’t be to hard now.

    The main reference which helped me the most in reaching this solution was:
    http://guides.rubyonrails.org/form_helpers.html
    If you are new to rails, and confused about forms, read this. I feel like a master now. Rails devs are really good at documenting things!

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

Sidebar

Related Questions

I've seen this question answered here for Rails 2, but not Rails 3. I
I'm new to rails, but not to programming. I'm trying to create a Case
I'm not new to Rails or Rspec, but I'm new to making gems. When
Hey, i have some experience with MVC. but I'm new to rails. I'm using
I am a new Ruby on Rails user and had a question. I have
I am very new to Rails and Cucumber so this may or may not
I'm totally new to Ruby but not to programming. All I did was going
I'm super new to programming and rails. I have a table of venues and
I am relatively new to ruby on rails, so this question might be easy.
I am not new to programming, but kind of rusty in Javascript/web development. I

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.