我在两个不同领域的模型上有两个不同的计算方法.在保存之前.
Class Event < ActiveRecord::Base
#some relations
before_save :method1, unless: :some_field_1?
before_save :method2, unless: :some_field_2?
def method1
some_field_1 = some_other_field / 2
end
def method2
some_field_2 = some_field_1 / 3
end
end
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是当调用method2时,some_field_1为null.我的猜测是,声明像我正在做的before_save回调是错误的.
我知道我可以将两个方法包装成一个没有条件和问题解决的方法,但我更愿意有条件回调.我想知道正确的做法.文档对此并不十分清楚.
非常感谢!
编辑
备查.代码还可以.问题出在其他地方(数据库)!
我知道beforeSave()YII中的函数功能,该函数用于执行某些操作,我们希望在保存数据之前执行该操作.
但是,就我们想要在我们的数据保存到数据库之前实现这一点,我们不能在save()调用之前直接编写这段代码( - > save()将记录存储到数据库)
因此,我不确定为什么我们需要创建像beforeSave()这样的特定函数来执行我们需要在Save()调用之前触发的操作,当我们在save()行之前直接编写该代码时.
有人可以解释一下吗?我已经搜索了很多,但是在每一页上,它都只重定向到beforeSave()函数的解释.
before_save :date_started_sets_deadline, if date_started.present?
Run Code Online (Sandbox Code Playgroud)
我不希望它before_save运行 if :date_started == nil。我已经尝试了上述行的各种版本,所以不确定是否必须更改它或方法本身。
def date_started_sets_deadline
if self.date_started > Date.tomorrow
self.deadline = self.date_started
end
end
Run Code Online (Sandbox Code Playgroud)
NoMethodError (undefined method '>' for nil:NilClass): app/models/challenge.rb:35:in 'date_started_sets_deadline'每当用户尝试创建挑战时,我都试图避免该错误date_started
我正在使用以下 VBA 代码在保存 Word 文档时显示一个消息框,
Public WithEvents appWord as Word.Application
Private Sub appWord_DocumentBeforeSave _
(ByVal Doc As Document, _
SaveAsUI As Boolean, _
Cancel As Boolean)
Dim intResponse As Integer
intResponse = MsgBox("Do you really want to " _
& "save the document?", _
vbYesNo)
If intResponse = vbNo Then Cancel = True
End Sub
Run Code Online (Sandbox Code Playgroud)
这段代码是在一个类中编写的。但这不起作用。保存时没有任何反应。这里有什么问题?
我有这个简单的模型:
class Post < ApplicationRecord
after_create_commit :process
before_save :re_process, on: :update
has_one :processed, class_name: 'Post::Process'
def process
self.processed.destroy if self.processed
processed = self.build_processed
processed.save!
end
private
def re_process
self.process if self.title_changed?
end
end
Run Code Online (Sandbox Code Playgroud)
Stack level to deep每当我创建一个新的时候我都会收到错误Post.
现在当我删除before_save :re_process, on: :update一切正常.
这条线不应该只在我更新帖子时受到影响吗?
在我的模板模型中,我有这个回调:
before_save :set_status, if: :is_template?
private
def is_template?
return self.template_type == 'template'
end
Run Code Online (Sandbox Code Playgroud)
如何更改它,使其仅在 template_type 不是“模板”时触发?
我尝试过这些:
1 before_save :set_status, if: !:is_template?
2 before_save :set_status, if: !(:is_template?)
Run Code Online (Sandbox Code Playgroud)
但它们都会导致“before_save找不到方法”错误。
读完这个问题后,我也尝试过这个:
before_save :set_status, if: Proc.new {|model| !model.is_template? }
Run Code Online (Sandbox Code Playgroud)
但对于这样一个简单的案例来说,这似乎有些过分了。
我真的需要编写另一种方法:is_not_template?才能使其工作吗?
我是Ruby的新手.我正在做Michael Hartl的Ruby on Rails教程,并在用户的模型中使用以下代码:
before_save { self.email = email.downcase }
Run Code Online (Sandbox Code Playgroud)
在这种情况下,可以接受写:
before_save { self.email.downcase! }
Run Code Online (Sandbox Code Playgroud)
或者由于某种原因这是否有缺陷?如果是的话,你能给我一个快速解释原因吗?
before-save ×7
ruby ×2
activerecord ×1
callback ×1
hook ×1
methods ×1
ms-word ×1
php ×1
save ×1
stack-level ×1
string ×1
vba ×1
yii ×1