从另一个模型访问值而不在Rails中创建当前模型中的字段

时间:2016-02-22 08:15:58

标签: ruby-on-rails

我有两个模型,IssueProductionReport。我想创建一个包含IssueProductionReport字段的表单。 Issues的值来自数据库,ProductionReport的值是新的。最后,当用户点击提交按钮时,所有值都应存储在ProductionReport表中。我有以下控制器:

 def index_productionReport
   @issue = Issue.all
   @issue = Issue.paginate(page: params[:page], per_page:10).order("created_at DESC")
 end

 def add_productionReport
   @production_report=ProductionReport.new
   @issue=Issue.find(params[:id])
 end

 def get_production
   @issue = Issue.find(params[:id])
   @production_report = ProductionReport.new(production_report_params)
   @production_report.save
   flash[:notice] = "production Saved Successfully!!!"
   redirect_to :action => "index_productionReport"
 end

我的观点是:

<%= form_for @production_report, :url=>{:controller=>"users",:action=>"get_production" } do |i|%>
<%= fields_for @issue do |i| %>
<tr><td><%= i.text_field :order_no,:required => true,:autofocus=>true %></td></tr>
<tr><td><%= i.text_field :issue_slip_no,:required => true %></td></tr>
<%end%>
<tr><td><%= i.label  :finished_goods_name,"finished goods name"%></td></tr>
<tr><td><%= i.text_field :finished_goods_name,:required => true %></td></tr>
<tr><td><%= i.label  :total_no_of_items_produced,"total no of item"%></td></tr>
<tr><td><%= i.text_field :total_no_of_items_produced,:required => true  %></td></tr>
<tr><td><%= i.label  :weight_per_item,"weight per item" %></td></tr>
<tr><td><%= i.text_field :weight_per_item,:required => true %></td></tr>
<tr><td><%= i.label  :total_weight_consumed,"total weight consumed" %></td></tr>
<tr><td><%= i.text_field :total_weight_consumed,:required => true %></td></tr>
<%= i.submit "save" %>
<%end%>

有人请帮助我实现我的需要..

更新: 流: 我在index_productionReport上有问题列表,每个问题都有“添加”链接。当用户单击add时,add_productionRepor将显示来自Issue表的2个值,剩余的空字段将用于ProductionReport。现在,当用户单击“保存”按钮时,所有值都应存储在ProductionReport表中。

1 个答案:

答案 0 :(得分:0)

根据您提供的信息,ProductionReport属于IssueIssue可能有ProductionReport个。在这种情况下,我会将issue_id存储在production_reports表中,这被视为标准化方式,而不是某些信息(order_noissue_slip_no),这被视为一种非规范化的方式。

无论Issue对于给定ProductionReport我需要哪些信息,我都可以直接从Issue查询。例如:@production_report.issue.order_no@production_report.issue.issue_slip_no。如果您想隐藏实施细节,您甚至可以委派order_noissue_slip_no,如下所示:

class Issue < ActiveRecord::Base
  has_many :production_reports
end

class ProductionReport < ActiveRecord::Base
  belongs_to :issue

  delegate :order_no, :issue_slip_no, :to => :issue
end 

de-normalized way会导致重复,从而导致数据一致性。想象一下,有人在创建order_no后更新issue_slip_no的{​​{1}}和Issue。它们都包含两种不同的信息。

尽管如此,如果您需要ProductionReport order_no直接在issue_slip_no存储Issue,由于某些其他业务要求,您可以使用以下内容视图:

ProductionReport

最后,我不会将所有操作推到<%= form_for @production_report, :url=>{:controller=>"users", :action=>"get_production" } do |i|%> <tr><td><%= i.text_field :order_no, :required => true, :readonly => true, value => @issue.order_no, :autofocus=>true %></td></tr> <tr><td><%= i.text_field :issue_slip_no, :readonly => true, value => @issue.issue_slip_no, :required => true %></td></tr> <tr><td><%= i.label :finished_goods_name, "finished goods name" %></td></tr> <tr><td><%= i.text_field :finished_goods_name, :required => true %></td></tr> <tr><td><%= i.label :total_no_of_items_produced,"total no of item" %></td></tr> <tr><td><%= i.text_field :total_no_of_items_produced,:required => true %></td></tr> <tr><td><%= i.label :weight_per_item,"weight per item" %></td></tr> <tr><td><%= i.text_field :weight_per_item,:required => true %></td></tr> <tr><td><%= i.label :total_weight_consumed,"total weight consumed" %></td></tr> <tr><td><%= i.text_field :total_weight_consumed,:required => true %></td></tr> <%= i.submit "save" %> <%end%> ,而是按照以下方式进行操作:

UsersController