2012-02-26 148 views
0

我有兩個表salary_structures和payheads。下面是我的表結構:唧唧to same more more more single single single single single single single single single single single more more??

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表,並在我的payheads表已經payhead_id我有四個payheads(基本,PF等) 。現在當一個管理員想要定義一個員工的salary_structure時,他必須爲每個支付頭寸確定金額,按照我的設計,我想一次爲一個salary_structure保存一個員工的所有支付頭。但問題出在一個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 %> 

,當我試圖正常保存它,它不保存payhead ID在salary_structures表 ,我應該做的事的模型。任何幫助,都會提前致歉。

回答

-1

喜的朋友我已經找到新路通過使用另一個來解決它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新payhead,

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

嗨,感謝您的解決方案,但我不想在payhead表中保存任何payhead,我想用所有這些payheads保存salary_structure,爲了方便我的金額字段在salary_structure表中不在支付表 – Ravindra 2012-02-27 06:57:43

+0

If你不想在匯款表中保存任何付款頭,那麼你打算在其中存儲什麼? salary_structure.amount是所有payhead.amounts的總和嗎?你如何更新它? – Rahul 2012-02-27 08:07:20

+0

先生,我先前告訴我的支付方式是爲公司所有員工管理員定義的。在薪金結構中,我只爲所有各自的支付人節省了金額,我可以從支付表中取回 – Ravindra 2012-02-27 09:21:30