2012-02-26 4 views
0

私は2つのテーブルsalary_structuresとpayheadsを持っています。以下は私のテーブル構造です:Hawは同じテーブルの単一IDの複数のレコードを保存しますか?

create_table "payheads", :force => true do |t| 
    t.integer "company_id",    :null => false 
    t.integer "defined_by",    :null => false 
    t.string "payhead_type" 
    t.string "payhead_name" 
    t.string "under" 
    t.string "affect_net_salary" 
    t.string "name_appear_in_payslip" 
    t.string "use_of_gratuity" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

と:

create_table "salary_structures", :force => true do |t| 
    t.integer "company_id",               :null => false 
    t.integer "for_employee" 
    t.integer "created_by" 
    t.date  "effective_from_date" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.integer "payhead_id" 
    t.decimal "amount",    :precision => 18, :scale => 2, :default => 0.0 
    end 

私は私のsalary_structuresテーブルにpayhead_idていると私のpayheadsテーブルに私は4 payheads(基本、Pfとなど)を持っている私のテーブル構造を1として。今、管理者が従業員のsalary_structureを定義したいときは、私のデザインごとに1つのsalary_structureの従業員のすべてのpayheadsを一度に保存したいと思うので、すべてのpayheadの金額を修正する必要があります。しかし、ここではsalary_structure idどのように私は複数のペイヘッドを保存することができます。以下は、私の見解形態である:

<%= form_for(@salary_structure) do |f| %> 
    <%= render 'shared/form_error', :object => @salary_structure %> 

     <div class="field"> 
      <%= f.label :for_employee, :class => "required" %><br /> 
      <%= collection_select(:salary_structure, :for_employee, @users, :id, :first_name,:autofocus =>"autofocus", :prompt => true) %> 
      </div> 

    <div class="column width3"> 
      <div class="field"> 
      <%= f.label :effective_from_date, :class => "required" %><br /> 
      <%= f.text_field :effective_from_date %> 
      </div> 
    </div> 
    <div class="column width6 first"> 
     <table class=" display stylized full" id="salary_structure_report" style=""> 
     <thead> 
      <tr> 
      <th width="80%"><label class="required">Pay Head</label></th> 
      <th width = "20%"><label class="required">Amount</label></th> 
      </tr> 
     </thead> 
      <tbody> 
       <% for ph in @payheads %> 
        <tr> 
        <td width = "80%"> 
         <%= f.hidden_field "payhead_id", ph.id %> 
         <%= ph.payhead_name %> </td> 
        <td width = "20%" ><%= f.text_field :amount %></td> 
        </tr> 
       <% end %> 
       </tbody> 
       <tfoot> 
        <tr> 
        <td class="ta-right" width = "80%">Total</td> 
        <td class="ta-left" width = "20%"><span class="WebRupee">Rs</span><span id = "total">00.00</span></td> 
        </tr> 
       </tfoot> 
     </table> 
    </div><br /> 
     <div class = "column width3 first"> 


       <button type="submit" class="btn btn-green big"><img src="/images/add.png" class="icon" /> Save</button>&nbsp; 

      <%= link_to 'Cancel', salary_structures_path, :class=>"btn btn-gray bg"%> 
     </div> 
    </div> 
<% end %> 

私は通常、それを保存しようとしたとき、私はモデルに何かをしなければならない、それは、salary_structuresテーブル にpayhead IDを保存しません。どんな助けでも、事前にお礼を申し上げます。

答えて

-1

こんにちは友人のための新しいpayheadを作成それを使用して解決するためにrテーブルを "line_item"と呼び、私のコントローラに ".build"を使用していました。私のフォームでは、ネストされたフォームを使用しています。

1
app/models/payhead.rb 

    belongs_to :salary_structure 

app/models/salary_structure.rb 

    has_many :payheads 

db/migration/create_payheads.rb 

    create_table 'payheads', :force => true do |t| 
    # define other columns 
    t.integer salary_structure_id 
    end 

db/migration/create_salary_structure.rb 

    # remove t.integer payhead_id 

アクセス互いにによって、

Payhead.find(some_payhead_id).salary_structure 

または

SalaryStructure.find(some_salary_structure_id).payheads 

既存salary_structure、私は新しい方法を発見した

SalaryStructure.find(some_salary_structure_id).payheads.create(params[:payhead]) 
# params[:payhead] is part of the form data 
+0

こんにちは、ありがとう、私はpayheadテーブルにpayheadを保存したくない、私はこれらすべてのpayheadsと利便性のために私のamountフィールドsalay_structureテーブルpayheadテーブルでないsalary_structureを保存する – Ravindra

+0

ペイヘッドテーブルにペイヘッドを保存したくない場合、それに正確に何を保存する予定ですか? salary_structure.amountはすべてのpayhead.amountの合計ですか?どのように更新しますか? – Rahul

+0

私は前もって会社のすべての従業員のための管理者として定義された私のpayheadsを言った。給与の構造では、私はそれぞれのpayheadsのための金額を保存し、私はpayheadテーブルからそれらをフェッチすることができます – Ravindra

関連する問題