gpt4 book ai didi

ruby-on-rails - user.save 上的堆栈级别太深

转载 作者:行者123 更新时间:2023-12-02 09:31:59 25 4
gpt4 key购买 nike

我想在创建用户时为我的用户分配一个确认码。在保存-更新它们之前,我还在一些列上加了标题。所以我的 user.rb 看起来像这样(可能有点乱):

// user.rb
*** some code ***
before_save { titleize_column(:name)
titleize_column(:surname)
capitalize_column(:complaints)
capitalize_column(:education)
capitalize_column(:job)
capitalize_column(:complaintsdetails)
capitalize_column(:prediagnosis)
capitalize_column(:existingdiagnosis)
capitalize_column(:knownilnessesother)
capitalize_column(:usedmedicine)
capitalize_column(:operation)
capitalize_column(:trauma)
capitalize_column(:allergy)
capitalize_column(:otherhabits)
capitalize_column(:motherother)
capitalize_column(:fatherother)
capitalize_column(:siblingsother)
}
before_save :generate_confirmation_code
protected
def generate_confirmation_code
unless self[:confirmed]
if(self[:type] == 'Patient')
update_attribute :confirmation_code, SecureRandom.urlsafe_base64(20)
update_attribute :confirmed, false
else
update_attribute :confirmed, true
end
end
end

protected
def capitalize_column(attr)
unless self[attr].nil?
self[attr] = Unicode::capitalize self[attr]
end
end

protected
def titleize_column(attr)
unless self[attr].nil?
words = self[attr].split
words.each_with_index do |v,i|
words[i] = Unicode::capitalize v
end
self[attr] = words.join(" ")
end
end

我使用单独的方法对列进行标题化和大写,因为在首次创建用户时它们可能为 nil,因此我在这些方法中检查它是否为 null。这种结构适用于具有强参数的正常注册。但是,如果我尝试使用下面的方法使用 Twitter 注册,它会给我错误“堆栈级别太深”,我可以看到它从应用程序跟踪中调用了 generate_confirmation_code 123 次,然后这些发生:

app/models/user.rb:83:in each'
app/models/user.rb:83:in
each_with_index'app/models/user.rb:83:in titleize_column'
app/models/user.rb:20:in
block 在'app/models/user.rb:64:in generate_confirmation_code'(x123 次)
app/models/user.rb:101:in
from_omniauth'app/controllers/socials_controller.rb:4:在“创建”中

// method for signing up/logging in a user from twitter

class << self
def from_omniauth(auth_hash)
if exists?(uid: auth_hash['uid'])
user = find_by(uid: auth_hash['uid'])
else
user = find_or_create_by(uid: auth_hash['uid'], provider: auth_hash['provider'], type: 'Patient')
user.password_digest = User.digest('111111')
user.name = auth_hash['info']['name']
user.location = get_social_location_for user.provider, auth_hash['info']['location']
user.avatar = auth_hash['info']['image']
user.url = get_social_url_for user.provider, auth_hash['info']['urls']
user.save! // THIS IS THE LINE 101!
conversation = Conversation.create()
user.conversation = conversation
admin = Admin.first
admin.conversations << conversation
user.progress = Progress.create(active_state:1)
end
user
end

我认为我没有正确使用 before_save 搞砸了,但不知道如何正确使用。我在这里做错了什么?

最佳答案

update_attribute 还会触发 save 回调,从而无限循环 before_save,从而产生太深的堆栈级别。

您可以在 before_save 回调方法中简单地分配值,因为无论如何它们都会在之后简单地保存。请参阅以下内容:

def generate_confirmation_code
unless self[:confirmed]
if(self[:type] == 'Patient')
self.confirmation_code = SecureRandom.urlsafe_base64(20)
self.confirmed = false
else
self.confirmed = true
end
end
end

关于ruby-on-rails - user.save 上的堆栈级别太深,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32114338/

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