Ruby on rails 如何将参数传递给Rails“;“代表”吗;?
目前我有: class Group < ActiveRecord::Base delegate :publish_group_creation, :to => 'MyAppModules::Publisher' after_create :publish_group_creation end 类组Ruby on rails 如何将参数传递给Rails“;“代表”吗;?,ruby-on-rails,Ruby On Rails,目前我有: class Group < ActiveRecord::Base delegate :publish_group_creation, :to => 'MyAppModules::Publisher' after_create :publish_group_creation end 类组'MyAppModules::Publisher' 创建后:发布组创建 结束 问题是,Publisher.publish\u group\u creation收到一个参数(即
Publisher.publish\u group\u creation
收到一个参数(即将发布的组)
我试过这样的东西
delegate:publish\u group\u creation,:to=>MyAppModules::Publisher,:group=>self
但它不起作用,使用
委托传递参数的正确方法是什么?我不确定这是否适用于您的场景,但您可以尝试重新处理一下。差不多
module MyAppModules::Publisher
def publish_group_creation
...
end
end
class Group < ActiveRecord::Base
include MyAppModules::Publisher
after_create :publish_group_creation
end
模块MyAppModules::Publisher
def发布组创建
...
结束
结束
类组
通过回调调用的方法不会在Rails中接收参数
试试这个:
class Group < ActiveRecord::Base
after_create :publish_group
private
def publish_group
MyAppModules::Publisher.publish_group_creation(self)
end
end
类组
如果发布者模块的方法都适用于您的群组模型,那么您将希望使用Mike的答案。我假设群组和发布者基本上是不相关的。谢谢,这就是我目前的代码,如果我们可以内联传递它们就好了!