【发布时间】:2012-08-10 04:02:39
【问题描述】:
我有两个相互链接的模型,我正在尝试做一个 after_save,在模型中创建,代码如下。
class CustomerBill < ActiveRecord::Base
after_save :updating_and_creating_ledger_items
has_many :customer_ledgers, :dependent => :destroy
def updating_and_creating_ledger_items
CustomerLedger.create(:date => self.date, :customer_id => self.customer_id, :user_id => self.user_id)
end
end
客户分类帐模型
class CustomerLedger < ActiveRecord::Base
belongs_to :customer_bill, :foreign_key => "customer_bill_id"
end
现在的问题是程序执行完美,但值没有放入数据库。如果我检查客户分类帐,它仍然是空的。
值没有被存储。似乎是什么问题?对此事的指导会有所帮助。
提前致谢。 :)
【问题讨论】:
-
尝试将创建更改为创建!让我知道会发生什么
-
当我创建的时候!表单没有被保存,它去编辑,我收到了我提交的错误,因为找不到 CustomerBill with id=30
-
现在我添加了 :customer_bill_id => self.id 并且表单没有得到保存..!!
标签: ruby-on-rails ruby-on-rails-3 activerecord model