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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:39:22+00:00 2026-06-04T00:39:22+00:00

I have an invoice controller and two model invoice and invoice_line_items and create a

  • 0

I have an invoice controller and two model invoice and invoice_line_items and create a tax_line_items using invoice_line_item class. Below is my code :

Here is my controller code :

class InvoicesController < ApplicationController
def new
    @menu = 'Income'
    @page_name = 'Create new invoice'
    @invoice = Invoice.new
    @invoice.invoice_line_items.build
    @invoice.tax_line_items.build

    respond_to do |format|
      format.html
      format.xml { render :xml => @invoice }
    end
  end
end

My model for invoice:

class Invoice < ActiveRecord::Base

  has_many :invoice_line_items
  has_many :tax_line_items, :class_name => "InvoiceLineItem",:dependent => :destroy

  accepts_nested_attributes_for :invoice_line_items, :reject_if => lambda {|a| a[:account_id].blank? } , :allow_destroy => true
  accepts_nested_attributes_for :tax_line_items, :reject_if => lambda {|a| a[:account_id].blank? } , :allow_destroy => true
end

and in my forms i used :

<%= form_for(@invoice) do |f| %>
<%= render 'shared/form_error', :object => @invoice %>
<% @invoice.invoice_line_items.each_with_index do |invoice_line_item, index| %>
                   <%= render "invoice_line_items", :invoice_line_item => invoice_line_item, :index => index %>
                <% end %>
<% @invoice.tax_line_items.each_with_index do |tax_line_item, tax_index| %>
                      <%= render "tax_line_items", :tax_line_item => tax_line_item, :tax_index => tax_index %>     
                <% end %>
<%end%>

partials :

1) invoice_line_items:

<tr id="row<%= index %>" valign="top" >
            <%= hidden_field_tag "invoice[invoice_line_items_attributes][#{index}][id]",invoice_line_item.id%>
            <td valign="top">
                <%= select_tag "invoice[invoice_line_items_attributes][#{index}][account_id]", options_from_collection_for_select(@from_accounts, :id, :name,:selected => invoice_line_item.account_id ), :include_blank => true, :class=>"full"  %>
              <!-- <a href="/accounts/new?account_head_id=10" > New item</a> -->
            </td>
      <td><%= text_area_tag "invoice[invoice_line_items_attributes][#{index}][description]",invoice_line_item.description, :class => "full", :cols => 10, :rows=>1 %></td>
      <td><%= text_field_tag "invoice[invoice_line_items_attributes][#{index}][quantity]",invoice_line_item.quantity, :class => 'full', :id => 'quantity', :onkeydown => "return numbersOnly(event);", :size => 5, :maxlength => 25 %></td>
      <td><%= text_field_tag "invoice[invoice_line_items_attributes][#{index}][unit_rate]",invoice_line_item.unit_rate, :class => 'full', :id => 'unit_cost', :onkeydown => "return numbersOnly(event);", :size => 5, :maxlength => 20 %></td><!--Jquery code is in application.js-->
      <td><%= text_field_tag "invoice[invoice_line_items_attributes][#{index}][discount_percent]", invoice_line_item.discount_percent, :class => 'full', :id => 'discount', :onkeydown => "return numbersOnly(event);", :maxlength => 5, :size => 5%></td>
      <td><%= text_field_tag "invoice[invoice_line_items_attributes][#{index}][amount]", invoice_line_item.amount, :class => 'full', :id => 'total', :readonly => 'readonly',  :size => 5%></td>
            <td><%= link_to image_tag("/images/black_icon/ic_cancel.png"),{:action => :remove_line_item, :index => index}, :remote => true unless index == 0 %></td>
    </tr>

and 2) tax_line_items:

<tr id="tax_row<%= tax_index %>" valign="top" >
   <%= hidden_field_tag "invoice[tax_line_items_attributes][#{tax_index}][id]",tax_line_item.id%>
   <%= hidden_field_tag "invoice[tax_line_items_attributes][#{tax_index}][tax]", tax_line_item.tax, :value => 1 %>
   <td style="background:#EDF4FF"></td>
   <td  class="ta-right" style="background:#EDF4FF" colspan="2"><label>Add Tax:</label></td>
   <td class="ta-right"  colspan="2" style="background:#EDF4FF">
      <%= select_tag "invoice[tax_line_items_attributes][#{tax_index}][account_id]", options_from_collection_for_select(@tax_accounts, :id, :name,:selected => tax_line_item.account_id ), :include_blank => true, :class=>"full"  %>
      <!-- <a href="/accounts/new?account_head_id=10" > New item</a> -->
   </td>
   <td style="background:#EDF4FF"><%= text_field_tag "invoice[tax_line_items_attributes][#{tax_index}][amount]", tax_line_item.amount, :class => 'full', :id => 'tax', :onkeydown => "return numbersOnly(event);", :size => 5%></td>
   <td style="background:#EDF4FF"><%= link_to image_tag("/images/black_icon/ic_cancel.png"),{:action => :remove_tax_item, :tax_index => tax_index}, :remote => true %></td>

</tr>

I can add new line item entry easily and edit it when their is no tax item. The problem is
when their is an tax item created in any entry and later try to edit this entry then my line items row appears multiple time for example suppose i have a tax item them a new row will appear for invoice line item ans an extra row for tax line items. i think this will happen because i have used a common table for both the line items. I did this because i have to save both line items entry in same table as both accounts came from same table.
I am very thankful if anyone have the right answer.Thanks a lot

  • 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-04T00:39:24+00:00Added an answer on June 4, 2026 at 12:39 am

    As this answer suggests, you need to add a ‘type’ column to this table to distinguish between invoice and tax items. It will remove a lot of complications from your code as Rails will do the work of identifying which record belongs to which model class from the common table.

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

Sidebar

Related Questions

I have two classes Invoice and InvoiceItem. I would like Invoice to have a
So, I have an Invoice object like so: public class Invoice { public virtual
I have an Invoice model in a Rails app. The Invoice has_many Products and
Let model Quote have attributes [price, description] Let model Invoice have attributes [price, description,
I am newbie to class and objects.Here I am making an application for invoice.So
Let's say I have a User model, and an Invoice model with a belongs_to
I have an Invoice model with a boolean field: is_overdue . This field is
I have /invoices/ controller and @service object. When /services/1/invoices/new is called, a new @invoice
I have taken following code from my template file, <a onclick=previewPop('<?= $this->url(array('controller'=>'report', 'action'=>'generatepreview','report_date'=>str_replace(%2F, /,
I'm using ActiveAdmin and Formtastic. I have an invoice form that has a drop

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.