保存记录...但不保存... :( [英] Saving a record.. but not saving... :(

查看:50
本文介绍了保存记录...但不保存... :(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于创建多个记录的模型的静态方法.保存父记录时调用此方法,将创建合适的 n 个子记录的数据传递给此方法(new_record),然后该方法遍历传递给它的数组,并创建子记录.

I have this static method for a model, which is creating multiple records. This method is called when saving the parent, the data for creating appropriate n child records is passed to this method (new_record) which then iterates through the array passed to it, and creates the child records.

工作有许多工作活动.该方法new_record属于job_activities模型,在job_controller中保存作业记录时调用.基本上试图以单一形式实现两个模型.作业has_many"job_activities 和 Job_activites属于"作业.

Job has many Job Activities. This method new_record belongs to job_activities model and is invoked in job_controller, when saving job record. Basically trying to implement two models in a single form. Job "has_many" job_activities, and Job_activites "belongs_to" a job.

这是我目前整理的内容 -

Here's what I have put together so far -

       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...

Weird thing happening is, that the before and after assignment state of the object ja, reflects the assignment of "created_by" but not of job_id_sk and job_activity...

而且,按照建议,我实现了保存!但这并没有引发任何异常......它仍然默默地表现着它所做的...... :(

And, as suggested, I implemented the save! but that didnt raise any exceptions.. its still quite silently behaving what its doing... :(

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值......

However, when I go and check the mysql table where the data should have been saved, I see that only NULL values have been passed in...

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)

我是否遗漏了什么?

推荐答案

发现问题,解决方案...

Found the problem, and the solution...

我使用 attr_accessor 作为模型,而我应该使用 attr_accessible...

I was using attr_accessor for the model, whereas I should have been using attr_accessible...

James 对这个问题的回答帮助了我..

Response from James on this question helped me..

调用.save在 ruby​​ 对象上只为所有字符串传递 nil

这篇关于保存记录...但不保存... :(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆