具有两种类型值的一个模型的活动记录has_many关联

时间:2012-03-28 12:12:29

标签: ruby-on-rails-3 activerecord

我正面临新的局面。我知道有人肯定遇到过这种情况:

我有一张发票表和invoice_line_items表。到目前为止,所有事情都很好用has_many并属于协会。现在我想在发票项目中添加税务帐户,因为我不想创建单独的表格,因此我在发票模型中进行了以下更改:

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

在我的控制器里我做了:

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

以我的形式我做了: 税务帐户

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

和发票项目:

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

以下是我的部分内容:  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>

我的座右铭是在invoice_line_items表的account_id栏中保存项目帐户和税务帐户,我希望有些好友有答案,提前谢谢

2 个答案:

答案 0 :(得分:2)

您遇到的问题是@invoice.invoice_line_items@invoice.tax_line_items会返回相同的值,因为它们都使用相同的ID加入。

好像你可以使用Single Table Inheritance(你必须向下滚动才能找到API中的部分)。使用invoice_line_items表中的类型列,您可以拥有模型继承。

class InvoiceLineItem < ActiveRecord::Base
  belongs_to :invoice
end

class TaxLineItem < InvoiceLineItem
  belongs_to :invoice
end

然后您可以清除 Invoice

中的关联
class Invoice < ActiveRecord::Base
  has_many :invoice_line_items
  has_many :tax_line_items, :dependent => :destroy
end

答案 1 :(得分:0)

我得到了解决方案,无论我如何做到这一点:

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

和_tax_line_items的部分内容:

<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[tax_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"  %>

   </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><%= link_to 'Remove',{:action => :remove_tax_item, :index => tax_index}, :remote => true %></td>
</tr>

这对我有用....