2

如果我有一个名为“模板”的模型,其字段为“a、b、c、d、e、f、g”,并且我有一个“报告”模型,该模型具有唯一字段但也与“模板”具有相同的字段:“蓝色,红色,金色,绿色,a,b,c,d,e,f,g')。

在新报告的表单上说,有一个下拉列表可以选择一个模板,该列表的值将是一个模板 ID。因此,在 Reports 的创建操作中,我创建了一个新的 Report 对象,然后通过 id 找到选定的模板。

@report = Report.new(params[:report])
@template = find(params[:report][:template_id])

此时(考虑到@report 对象包括@template 对象所做的所有字段),是否有一种简洁的方法将@template 的值复制到@report 对象中?

谢谢!导轨 2.3.5 / Ruby 1.8.7

4

1 回答 1

3

最简单的方法:

@report = Report.new(params[:report])
@template = find(params[:report][:template_id])
@template.attributes = @report.attributes #this copies fields from report to template
于 2012-06-10T04:24:18.507 回答