Rails:来自模块的回调
我尝试这样做:
app/models/my_model.rb:
class MyModel < ActiveRecord::Base
include MyModule
...
end
lib/my_module.rb:
module MyModule
before_destroy :my_func #!
def my_func
...
end
end
但出现错误:
undefined method `before_destroy' for MyModule:Module
如何纠正它。
我也是红宝石新手。什么类型具有这些“属性”:before_destroy、validates、has_many? 它们是变量还是方法还是什么? 谢谢
I try to do this:
app/models/my_model.rb:
class MyModel < ActiveRecord::Base
include MyModule
...
end
lib/my_module.rb:
module MyModule
before_destroy :my_func #!
def my_func
...
end
end
but I get an error:
undefined method `before_destroy' for MyModule:Module
How can I correct it.
Also I'm new to ruby. What type has these "attributes": before_destroy, validates, has_many?
Are they variables or methods or what?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
before_destroy
、validates
等不是属性或类似的东西。这些是方法调用。在 ruby 中,类的主体都是可执行代码,这意味着类主体的每一行都由解释器执行,就像方法主体一样。
before_destroy :my_func
是一个常见的 ruby 方法调用。被调用的方法是before_destroy
,它接收符号:my_func
作为参数。在调用该方法的范围内的类(或模块)中查找该方法。因此,继续你的问题,我想现在你应该明白,当解释器加载你的模块时,
它开始执行其主体并在该模块中搜索方法
before_destroy
但找不到。您想要做的不是在模块上调用此方法,而是在包含该模块的类上调用此方法。为此,我们有一个使用Module#included
方法的常见习惯用法:before_destroy
,validates
, etc. are not attributes or anything like that. These are method calls.In ruby, the body of a class is all executable code, meaning that each line of the class body is executed by the interpeter just like a method body would.
before_destroy :my_func
is a usual ruby method call. The method that gets called isbefore_destroy
, and it receives a symbol:my_func
as an argument. This method is looked up in the class (or module) in the scope of which it is called.So moving on to your question, I think now you should understand that when the interpreter loads your module
it starts executing its body and searches for the method
before_destroy
in this module and cannot find one. What you want to do is call this method not on the module, but rather on the class where the module is included. For that we have a common idiom using theModule#included
method:在 lib/my_module.rb 中,执行以下操作:
在 app/models/my_model.rb 中,执行以下操作:
您尝试在上面创建的模块中没有 before_destroy 过滤器。我的代码所做的是创建一个将从 ActiveRecord::Base 继承的类,这将是您的所有其他类都可以继承的模板类。模板类还包含 ActiveRecord::Base 的所有属性。
In lib/my_module.rb, do this:
In app/models/my_model.rb, do this:
There is no before_destroy filter in the module you are trying to create above. What my code does is creating a class that will inherit from ActiveRecord::Base and that will be your template class which all your other classes can inherit from. The template class also contains all properties of ActiveRecord::Base.
您可以通过从 MyModule 中删除 before_destroy 并将其放置在 MyModel 中来纠正此问题,而不是
before_destroy 和其他回调仅适用于扩展 ActiveRecord::Base 的类,更多信息 这里
希望这有帮助 =)
u can correct this by removing before_destroy from MyModule and place it in MyModel instead
before_destroy and other callbacks are only available to classes which extends ActiveRecord::Base, more info here
hope this helps =)