我有一个模型的这个静态方法,它正在创建多个记录。该方法在保存父对象时调用,用于创建适当的 n 个子记录的数据被传递给该方法(new_record),然后迭代传递给它的数组,并创建子记录。
Job 有许多 Job 活动。此方法new_record 属于job_activities 模型,在job_controller 中调用,保存作业记录时。基本上试图以一种形式实现两个模型。工作“has_many”job_activities,而Job_activites“belongs_to”一个工作。
到目前为止,这是我整理的内容-
def self.new_record(sk_job_id, activities_list)
activities_list.each do | act |
puts "AAAAAAAAAAAAAAAAAAAAAAA creating a new record for "+act.inspect+" and " + sk_job_id.to_s
ja = JobActivity.new
puts "Job ID SK = "+sk_job_id.to_s
puts "act = "+act
puts "Record before assignment "+ ja.inspect
ja.job_id_sk = sk_job_id
ja.job_activity = act
ja.created_by = "raghav"
ja.updated_by = "raghav"
puts "Record after assignment "+ ja.inspect
ja.save!
puts "record after saving "+ ja.inspect
end
end
奇怪的是,对象 ja 的分配前后状态反映的是“created_by”的分配,而不是 job_id_sk 和 job_activity 的分配......
而且,正如建议的那样,我实施了保存!但这并没有引发任何例外..它仍然默默地表现着它在做什么...... :(
AAAAAAAAAAAAAAAAAAAAAAA creating a new record for "ACT1" and 40
Job ID SK = 40
act = ACT1
Record before assignment #<JobActivity id: nil, job_id_sk: nil, job_activity: nil, created_by: nil, updated_by: nil, created_at: nil, updated_at: nil>
Record after assignment #<JobActivity id: nil, job_id_sk: nil, job_activity: nil, created_by: "raghav", updated_by: "raghav", created_at: nil, updated_at: nil>
record after saving #<JobActivity id: 13, job_id_sk: nil, job_activity: nil, created_by: "raghav", updated_by: "raghav", created_at: "2013-01-08 02:22:30", updated_at: "2013-01-08 02:22:30">
但是,当我去检查应该保存数据的 mysql 表时,我看到只传入了 NULL 值......
mysql> select job_id_sk, job_activity from job_activities;
+-----------+--------------+
| job_id_sk | job_activity |
+-----------+--------------+
| NULL | NULL |
| NULL | NULL |
| NULL | NULL |
| NULL | NULL |
| NULL | NULL |
+-----------+--------------+
5 rows in set (0.00 sec)
我在某处遗漏了什么吗?