基本上,我有3个数据表。 “jobs”,“期间”和相关表格“JobPeriodAssociation”。
现在,JobPeriodAssociation显示作业和期间的所有可能组合。例如。我有2个作业(j1,j2)和2个句点(t1,t2),表格将显示4行:j1t1,j1t2,j2t1,j2t2。
如果我现在在“句点”表中添加句点,它会通过添加(空白)第3期/作业组合自动更新“JobPeriodAssociation”表。 .A。 j1t1,j1t2,j1t3,j2t1,j2t2,j2t3。
这是通过periods_controller
中的更改操作完成的def change
@mynumber_of_periods = params[:mynewnumber].to_i
@number_of_periods = Period.count
number_of_missing_periods=@mynumber_of_periods - @number_of_periods
if number_of_missing_periods > 0
@jobs = Job.all
@nanns = Nann.all
(1..number_of_missing_periods).each do |number|
name="t#{number+@number_of_periods}"
@new_period=Period.create!(name: name)
@jobs.each { |jo|
JobPeriodAssociation.create(job_id: jo.id, period_id: @new_period.id, demand: 1, wtp: 0, minage: 0, maxage: 0, genderjf: 0, genderjm: 0, genderji: 0, minval: 0, children: 0, dlj: 0, faj: 0, pcj: 0)
}
@nanns.each { |na|
NannPeriodAssociation.create(nann_id: na.id, period_id: @new_period.id, available: 1, wage: 0, age: 0, value: 0, genderf: 0, genderm: 0, childrenn: 0, dl: 0, fa: 0, pc: 0)
Matching.create(nann_id: na.id, period_id: @new_period.id)
}
end
end
(+显然路线和索引视图中有一些小的改动)
我对乔布斯做了同样的事情,这意味着更改作业数量会导致JobPeriodAssociation自动更改。
def change
@mynumber_of_jobs = params[:mynewnumber].to_i
@number_of_jobs = Job.count
number_of_missing_jobs=@mynumber_of_jobs - @number_of_jobs
if number_of_missing_jobs > 0
@periods = Period.all
(1..number_of_missing_jobs).each do |number|
name="j#{number+@number_of_jobs}"
@new_job=Job.create!(name: name)
@periods.each { |per|
JobPeriodAssociation.create(period_id: per.id, job_id: @new_job.id, demand: 1, wtp: 0, minage: 0, maxage: 0, genderjf: 0, genderjm: 0, genderji: 0, minval: 0, children: 0, dlj: 0, faj: 0, pcj: 0)
}
end
end
if number_of_missing_jobs < 0
(@mynumber_of_jobs +1 ..@number_of_jobs).each do |number|
name="j#{number}"
Job.find_by_name(name).destroy
end
end
现在的问题是:我还可以选择简单地创建一份新工作。
def new
@job = Job.new
end
创建新作业的相应表单如下所示:
<%= form_for(@job) do |f| %>
<% if @job.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@job.errors.count, "error") %> prohibited this job from being saved:</h2>
<ul>
<% @job.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :demand %><br>
<%= f.number_field :demand %>
</div>
.
.
.
<div class="field">
<%= f.label :pcj %><br>
<%= f.number_field :pcj %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
如果我创建一个新工作而不只是“更改”工作量,我希望此更改也适用于JobPeriodAssociation表,但似乎找不到方法。
非常感谢任何建议!
答案 0 :(得分:0)
您需要将此添加到创建作业的方法中:
def create
@job = Job.new(params[:job])
if @job.save
periods = Period.all
periods.each do |period|
@job.periods << period
end
# Do the usual rails redirect
end
end
为此,您的模型应该定义正确的关联。
答案 1 :(得分:0)
@new_job=Job.create!(name: name)
@periods.each { |per|
JobPeriodAssociation.create(period_id: per.id, job_id: @new_job.id, demand: 1, wtp: 0, minage: 0, maxage: 0, genderjf: 0, genderjm: 0, genderji: 0, minval: 0, children: 0, dlj: 0, faj: 0, pcj: 0)
}
但是,我想将创建的作业(包括其参数)推送到附加图像中显示的JobPeriodAssociation。你有什么想法吗?
再次,非常感谢你的帮助!