Ruby on rails 3 如何在同一个表中为单个id保存多条记录?
我有两张表:工资结构和工资总额。以下是我的表格结构:Ruby on rails 3 如何在同一个表中为单个id保存多条记录?,ruby-on-rails-3,Ruby On Rails 3,我有两张表:工资结构和工资总额。以下是我的表格结构: 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_n
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
以及:
根据我的表格结构,我在我的工资结构表中有工资头id,在我的工资头表中有四个工资头(基本工资、个人理财等)。现在,当一个管理员想要定义一个员工的工资结构时,他必须为每个工资头确定金额,根据我的设计,我想一次将一个员工的所有工资头保存为一个工资结构。但问题是,对于一个工资结构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>
<%= link_to 'Cancel', salary_structures_path, :class=>"btn btn-gray bg"%>
</div>
</div>
<% end %>
@工资结构%>
“必需”%>
“自动对焦”,提示=>true)%>
“必需”%>
工资总额
数量
全部的
Rs00.00
拯救
“btn btn灰色背景”%>
当我试图正常保存它时,它不会在salary_structures表中保存工资头id
,我应该在模型中做些什么吗。如果有任何帮助,请提前道歉和感谢
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.create(params[:payhead])
# params[:payhead] is part of the form data
嗨,朋友们,我找到了解决这个问题的新方法,使用了另一个名为“line_item”的表,并在我的控制器中使用了“.build”,它是如何工作的。在我的表单中,我使用了嵌套表单。您好,谢谢您的解决方案,但我不想在工资头表中保存任何工资头,我想用所有这些工资头保存一个工资头结构,为了方便起见,我的金额字段在工资头结构表中而不是在工资头表中如果您不想在工资头表中保存任何工资头,那你打算在里面储存什么呢?工资结构金额是所有工资总额金额的总和吗?你是如何更新的?先生,我之前告诉过你,我的工资是由公司所有员工的管理人员确定的。在工资结构中,我只为所有相应的工资头保存金额,我可以从工资头表中获取它们
SalaryStructure.find(some_salary_structure_id).payheads
SalaryStructure.find(some_salary_structure_id).payheads.create(params[:payhead])
# params[:payhead] is part of the form data