我想在after_save回調中使用最近存儲的記錄標識,如果是的話,那可能如何?在after_save回調中使用最近保存的記錄標識的Rails ActiveRecord
after_save :create_children
def create_children
self.id
end
更新: 對不起有什麼用我的保存功能去錯了,是我不好,對不起,浪費你的時間
感謝
我想在after_save回調中使用最近存儲的記錄標識,如果是的話,那可能如何?在after_save回調中使用最近保存的記錄標識的Rails ActiveRecord
after_save :create_children
def create_children
self.id
end
更新: 對不起有什麼用我的保存功能去錯了,是我不好,對不起,浪費你的時間
感謝
我剛試過這個:
class Thing < ActiveRecord::Base
after_save :test_me
def test_me
puts self.id
end
end
,並在控制檯:
$ rails c
Loading development environment (Rails 3.0.4)
>> x=Thing.new
=> #<Thing id: nil, name: nil, created_at: nil, updated_at: nil>
>> x.save
2
=> true
>> y=Thing.create
3
=> #<Thing id: 3, name: nil, created_at: "2011-04-27 15:57:03", updated_at: "2011-04-27 15:57:03">
還有什麼在你的模型是怎麼回事?
至少在MySql中,自動增量id列必須設置爲主鍵。我有一個問題,我的主鍵由兩個字段組成 - 所以ActiveRecord不會讀取Id。我將其更改爲ID字段,代碼開始工作。 – 2015-09-03 18:33:45