简体   繁体   English

Rails获取在控制器中创建的新记录的ID

[英]Rails get id of new record created in controller

I have jobplans that have many jobtasks . 我的工作jobplans有很多工作jobtasks These are like templates. 这些就像模板。 I want to create workorders and tasks from them. 我想从中创建工作workorderstasks

This is the code in the jobplans controller: 这是jobplans控制器中的代码:

def copy_to_workorder
  @jobplan = Jobplan.find(params[:id]) # find original jobplan
  wo_attrs =  @jobplan.attributes
  wo_attrs.delete('woschedule_id')
  Workorder.create(wo_attrs)

  @jobplan.jobtasks.each do |jobtask|
    jobtask_attrs = jobtask.attributes
    jobtask_attrs.delete('jobplan_id')
    jobtask_attrs.merge!({ workorder_id: @workorder.id })
    Task.create(jobtask_attrs)
  end

  redirect_to @jobplan, notice: 'Project was successfully created.'
end

My problem is the @workorder is nil. 我的问题是@workorder为nil。 I need each new task to refer to the workorder . 我需要每个新task来引用task workorder How do access the id for the newly created workorder ? 如何访问新创建的工作单的workorder

Thanks for the help! 谢谢您的帮助!

Do this: 做这个:

def copy_to_workorder
  @jobplan = Jobplan.find(params[:id]) # find original jobplan
  wo_attrs =  @jobplan.attributes
  wo_attrs.delete('woschedule_id')
  workorder = Workorder.create(wo_attrs)
  # ^^^^^ saving the newly created record in a local variable
  #       so we can refer to it later

  @jobplan.jobtasks.each do |jobtask|
    jobtask_attrs = jobtask.attributes
    jobtask_attrs.delete('jobplan_id')
    jobtask_attrs.merge!(workorder_id: workorder.id)
    #                                  ^^^^^^ now fetch its id
    Task.create(jobtask_attrs)
  end

  redirect_to @jobplan, notice: 'Project was successfully created.'
end

You're never instantiating @workorder (and you don't really need an instance variable). 您无需实例化@workorder (并且您实际上不需要实例变量)。 Just change: 只是改变:

 Workorder.create(wo_attrs)

to

 workorder = Workorder.create(wo_attrs)

and further on 并进一步

jobtask_attrs.merge!({ workorder_id: workorder.id })

GL & HF GL和HF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails多态关联如何将id和type传递给控制器​​并插入到新记录中 - rails polymorphic associations how do id and type get passed to the controller and inserted into a new record 如何以Rails方式获取新创建的记录的ID? - How to get the id of the newly created record in rails way? Rails控制器没有创建新记录 - Rails Controller not creating new record 如何在 rails 中获取新创建记录的 id 并将该 id 与另一个模型相关联 - Rails - How to get the id of newly created record in rails & associate the id with another model - Rails 创建记录之前,模型中的自我ID-rails 3 - Self ID in model, before record is created - rails 3 从rails中创建的记录中获取ID - getting Id from created record in rails Rails活动记录之间创建的特定ID - rails active record specific id created between 从创建的记录中获取ID并将其保存到控制器中的另一个记录 - Fetch ID from created record and save it to another record in controller 如何通过rails activerecord获取今天创建的记录? - How to get record created today by rails activerecord? Rails RSpec:控制器测试,检查是否错误如果由于验证错误而无法创建新记录,则用条目填充模型数组 - Rails RSpec: Controller Testing, checking if errors Array of model is filled with entries if new record cannot be created due to validation error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM