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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:10:48+00:00 2026-06-01T06:10:48+00:00

i am facing a new situation. I know someone definitely had faced these type

  • 0

i am facing a new situation. I know someone definitely had faced these type of situation:

I have an invoices table and an invoice_line_items table. Till now every thing is going well with has_many and belongs to association. Now i want to add tax accounts in invoice items, for that i don’t want to create a separate table, hence i make below changes in my invoice model :

invoice.rb:

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

    #validations
         validates_presence_of :invoice_line_items
    validates_associated :invoice_line_items 
end

invoice_line_item.rb:

class InvoiceLineItem < ActiveRecord::Base
  belongs_to :invoice
  end

and in my controller i did:

class InvoicesController < ApplicationController
def new

    @invoice = Invoice.new

        @invoice.invoice_line_items.build
    @invoice.tax_line_items.create 

        @from_accounts = TransactionType.fetch_from_accounts(current_company.id,'sales')
        @to_accounts = TransactionType.fetch_to_accounts(current_company.id, 'sales') 
    @tax_accounts = TransactionType.fetch_from_accounts(current_company.id, 'tax')

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

in my form i did:
for tax accounts

 <% @invoice.invoice_line_items.each_with_index do |invoice_line_item, tax_index| %>
                          <%= render "tax_line_items", :invoice_line_item => invoice_line_item, :index => tax_index %>     
                    <% end %>

and for invoice items:

<% @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 %>

and here are my partials:
1)_invoice_line_items.html.erb:

<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",:rows =>2 %></td>
      <td><%= text_field_tag "invoice[invoice_line_items_attributes][#{index}][quantity]",invoice_line_item.quantity, :class => 'amount', :id => 'quantity', :onkeydown => "return numbersOnly(event);", :size => 8, :maxlength => 25 %></td>
      <td><%= text_field_tag "invoice[invoice_line_items_attributes][#{index}][unit_rate]",invoice_line_item.unit_rate, :class => 'amount', :id => 'unit_cost', :onkeydown => "return numbersOnly(event);", :size => 8, :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 => 'amount', :id => 'discount', :onkeydown => "return numbersOnly(event);", :maxlength => 5, :size => 8%></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 'Remove',{:action => :remove_line_item, :index => index}, :remote => true unless index == 0 %></td>
    </tr>

2) _tax_line_items.html.erb:

<tr id="tax_row<%= tax_index %>" valign="top" >
   <%= hidden_field_tag "invoice[tax_line_items_attributes][#{tax_index}][id]",invoice_line_item.id%>
   <td></td>
   <td colspan="2" class="ta-right"><label>Add Tax:</label></td>
   <td class="ta-right"  colspan="2" style="background:#EDF4FF">
      <%= select_tag "invoice[invoice_line_items_attributes][#{tax_index}][account_id]", options_from_collection_for_select(@tax_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 style="background:#EDF4FF"><%= text_field_tag "invoice[invoice_line_items_attributes][#{tax_index}][amount]", invoice_line_item.amount, :class => 'full', :id => 'tax', :onkeydown => "return numbersOnly(event);", :size => 5%></td>
   <td><%= link_to 'Remove',{:action => :remove_tax_item, :index => tax_index}, :remote => true %></td>
</tr>

My motto is to save both items accounts and tax accounts in invoice_line_items table’s account_id column, i hope some buddy has the answer, thanks in advance

  • 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-01T06:10:49+00:00Added an answer on June 1, 2026 at 6:10 am

    The issue you have is @invoice.invoice_line_items and @invoice.tax_line_items will return the same values since they both are using the same id to join.

    Seems like you could use Single Table Inheritance (you will have to scroll down to find the section in the API). Using a type column in the invoice_line_items table, you can have model inheritance.

    class InvoiceLineItem < ActiveRecord::Base
      belongs_to :invoice
    end
    
    class TaxLineItem < InvoiceLineItem
      belongs_to :invoice
    end
    

    then you can cleanup you associations in Invoice

    class Invoice < ActiveRecord::Base
      has_many :invoice_line_items
      has_many :tax_line_items, :dependent => :destroy
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hi im new to jquery and im facing these problem i have these code
I am new to VB.net and facing a strange situation. I have a structure
I am new to MVC and facing one issue. I have a xml file
I'm confronted with a new issue, the situation I'm facing is as follows :
I am facing a situation in which I have a ListView item. When I
I'm pretty new to Knockout.js and am facing now a situation where I'm not
Well, I'm facing a new problem atm. I have two elements fixed to the
I am new to JUnit and facing difficulty in writing JUnit. I have an
I am relatively new to javascript and I am facing some difficulty.I have two
I'm quite new to Java and I'm facing a situation I can't solve. 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.