gpt4 book ai didi

ruby-on-rails - after_create 回调似乎无法正常工作

转载 作者:行者123 更新时间:2023-12-04 05:57:49 24 4
gpt4 key购买 nike

我有以下带有日志信息的代码。 global_id 值设置为 attr_accessible

这个有效

代码:

Location.update(model[:id],:global_id => gi[:id])

日志
before location save
Location Load (0.3ms) SELECT `locations`.* FROM `locations` WHERE `locations`.`id` = 280 LIMIT 1
(0.3ms) UPDATE `locations` SET `global_id` = 11490, `updated_at` = '2012-02-16 04:48:17' WHERE `locations`.`id` = 280
after location save

这个没有,我不知道为什么。有任何想法吗:

代码:
User.update(model[:id],:global_id => gi[:id])

日志:
here is more info
here i am and I am a user
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 31 LIMIT 1
(0.3ms) SELECT 1 FROM `users` WHERE (`users`.`email` = BINARY 'jj@jxx.com' AND `users`.`id` != 31) LIMIT 1
after my user update

任何想法为什么第二个不起作用?

谢谢

编辑#1
两种型号都有:
after_create SaveGlobalInfo

那个类是:
class SaveGlobalInfo < ActiveRecord::Base
def self.after_create(model)
gi=GlobalIdentification.create()
if (model.class.name=='Location')
puts "before location save"
Location.update(model[:id],:global_id => gi[:id])
puts "after location save"
elsif (model.class.name=='User')
puts "here i am and I am a user"
User.update(model[:id],:global_id => gi[:id])
puts "after my user update"
end

最佳答案

如果我不得不猜测那是因为你没有通过某种验证。使用第二个 SQL 调用执行的测试是为了确保您的 email 上的唯一性。 field 。如果这失败并且无法保存记录怎么办?

这特别奇怪:

after_create SaveGlobalInfo

您可以为回调创建辅助类,但我以前从未将它们视为实际的 ActiveRecord 模型。也许你的意思是定义一个模块?

关于ruby-on-rails - after_create 回调似乎无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9305778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com