Rails:来自模块的回调

发布于 2024-10-20 09:53:25 字数 468 浏览 2 评论 0原文

我尝试这样做:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

み格子的夏天 2024-10-27 09:53:25

before_destroyvalidates 等不是属性或类似的东西。这些是方法调用。

在 ruby​​ 中,类的主体都是可执行代码,这意味着类主体的每一行都由解释器执行,就像方法主体一样。

before_destroy :my_func 是一个常见的 ruby​​ 方法调用。被调用的方法是 before_destroy,它接收符号 :my_func 作为参数。在调用该方法的范围内的类(或模块)中查找该方法。

因此,继续你的问题,我想现在你应该明白,当解释器加载你的模块时,

module MyModule
  before_destroy :my_func    #!

  def my_func
    ...
  end
end

它开始执行其主体并在该模块中搜索方法 before_destroy 但找不到。您想要做的不是在模块上调用此方法,而是在包含该模块的类上调用此方法。为此,我们有一个使用 Module#included 方法的常见习惯用法:

module MyModule
  module InstanceMethods
    def my_func
      ...
    end
  end

  def self.included(base)
    base.send :include, InstanceMethods
    base.before_destroy :my_func
  end
end

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 is before_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

module MyModule
  before_destroy :my_func    #!

  def my_func
    ...
  end
end

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 the Module#included method:

module MyModule
  module InstanceMethods
    def my_func
      ...
    end
  end

  def self.included(base)
    base.send :include, InstanceMethods
    base.before_destroy :my_func
  end
end
醉南桥 2024-10-27 09:53:25

在 lib/my_module.rb 中,执行以下操作:

  class MyInheritedClass <  ActiveRecord::Base
    before_destroy :my_func
    def my_func
       ...
    end
  end

在 app/models/my_model.rb 中,执行以下操作:

class MyModel < MyInheritedClass
  ...
end

您尝试在上面创建的模块中没有 before_destroy 过滤器。我的代码所做的是创建一个将从 ActiveRecord::Base 继承的类,这将是您的所有其他类都可以继承的模板类。模板类还包含 ActiveRecord::Base 的所有属性。

In lib/my_module.rb, do this:

  class MyInheritedClass <  ActiveRecord::Base
    before_destroy :my_func
    def my_func
       ...
    end
  end

In app/models/my_model.rb, do this:

class MyModel < MyInheritedClass
  ...
end

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.

御弟哥哥 2024-10-27 09:53:25

您可以通过从 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 =)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文