如何在同一个表中为单个id保存多个记录?

时间:2012-02-26 12:40:49

标签: ruby-on-rails-3

我有两个表salary_structures和payheads。以下是我的表结构:

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表中,我有四个支付头(基本,Pf等)。现在,当管理员想要定义员工的薪资结构时,他必须为每个支付头确定金额,按照我的设计,我想立即为一个员工保存所有支付单一的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 %>

当我试图正常保存时,它不会在salary_structures表中保存payhead id 我应该在模特身上做点什么吗任何帮助,都会提前感谢道歉。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:-1)

朋友们,我已经找到了解决方法的新方法,使用另一个名为“line_item”的表,并在我的控制器中使用了“.build”,它有些工作方式。在我的表格中,我使用了嵌套表格。