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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T13:59:25+00:00 2026-05-21T13:59:25+00:00

I’m creating a billing application in rails3, I have a confusion when it comes

  • 0

I’m creating a billing application in rails3, I have a confusion when it comes to creating a bill.

one bill can have one or more items

'bill' has many 'items'
'item' belongs to 'bill'

my requirement is as follows

I should be able to create a new bill and add items to it (any number of items can be added)

my problem is

1 – to get items to be saved in the bill_details table I should first generate bill_id, What is the best way to generate this bill id.

2 – what is the best way to implement a scenario like this

3 – can i get any help from rails nested forms

thanks

cheers
sameera

  • 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-21T13:59:25+00:00Added an answer on May 21, 2026 at 1:59 pm

    You should use polymorphic association in this scenario. Here what you can do to achieve this:

    in bill.rb # model file:

    class Bill < ActiveRecord::Base
      has_many :items  # establish association with items!
      # to save items in bill only if they are there!
      accepts_nested_attributes_for :items, :allow_destroy => :true,
        :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
    end
    

    in item.rb # model file :

    class Item < ActiveRecord::Base
          belongs_to :bill  # establish association with bill!
    end
    

    In your bills_controller.rb create normal actions : index, new, create, show, edit, update, delete
    for update action:

    def update
        @bill = Bill.find(params[:id])
        respond_to do |format|
          if @bill.update_attributes(params[:bill])
            format.html { redirect_to(@bill, :notice => 'Bill was successfully updated.') }
            format.xml  { head :ok }
          else
            format.html { render :action => "edit" }
            format.xml  { render :xml => @bill.errors, :status => :unprocessable_entity }
          end
        end
      end
    

    and you don’t have to bother to create any action/ method in your items_controller.rb, just create a partial _form.html.erb in view/Items/_form.html.erb and put this:

        <%= form.fields_for :items do |item_form| %>
          <div class="field">
            <%= tag_form.label :name, 'Item:' %>
            <%= tag_form.text_field :name %>
          </div>
          <div class="field">
            <%= tag_form.label :quantity, 'Quantity:' %>
            <%= tag_form.text_field :quantity %>
          </div>
          <% unless item_form.object.nil? || item_form.object.new_record? %>
            <div class="field">
              <%= tag_form.label :_destroy, 'Remove:' %>
              <%= tag_form.check_box :_destroy %>
            </div>
          <% end %>
        <% end %>
    

    Now you have to call this from your view/bills/_form.html.erb:

    <% @bill.tags.build %>
    <%= form_for(@bill) do |bill_form| %>
      <% if @bill.errors.any? %>
      <div id="errorExplanation">
        <h2><%= pluralize(@bill.errors.count, "error") %> prohibited this bill from being saved:</h2>
        <ul>
        <% @bill.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
        </ul>
      </div>
      <% end %>
    
      <div class="field">
        <%= bill_form.label :name %><br />
        <%= bill_form.text_field :name %>
      </div>
    
      <h2>Items</h2>
      <%= render :partial => 'items/form',
                 :locals => {:form => bill_form} %>
      <div class="actions">
        <%= bill_form.submit %>
      </div>
    <% end %>
    

    View/Bills/new.html.erb:

    <h1>New Bill</h1>
    
    <%= render 'form' %>
    
    <%= link_to 'Back', bills_path %>
    

    View/Bills/edit.html.erb:

    <h1>Editing Bill</h1>
    
    <%= render 'form' %>
    
    <%= link_to 'Show', @bill %> |
    <%= link_to 'Back', bills_path %>
    

    Regards,
    Surya

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.