2009-11-24 59 views
1

目前我有这样的:如何将参数传递给Rails“委托”?

 
class Group < ActiveRecord::Base 

    delegate :publish_group_creation, :to => 'MyAppModules::Publisher' 
    after_create :publish_group_creation 

end 

的事情是,Publisher.publish_group_creation接收1个参数(这是要被公开组)

我想是这样的;

delegate :publish_group_creation, :to => 'MyAppModules::Publisher', :group => self

,但它不工作,有什么用delegate传递参数正确的方式?

回答

0

通过回调调用的方法在Rails中没有接收参数。

试试这个:

class Group < ActiveRecord::Base 
    after_create :publish_group 

    private 
    def publish_group 
    MyAppModules::Publisher.publish_group_creation(self) 
    end 
end 
+0

如果出版商模块的方法都将应用到您的集团模式,那么你会想要去与小李的回答。我假设小组和出版商大多不相关。 – PreciousBodilyFluids 2009-11-24 17:27:22

+0

谢谢,这就是我的代码,如果我们可以通过内联代码,这将是很好的! – jpemberthy 2009-11-24 21:35:50

0

我不知道这是否会为您的方案的工作,但你可以尝试返工这个有点。类似于

module MyAppModules::Publisher 
    def publish_group_creation 
    ... 
    end 
end 

class Group < ActiveRecord::Base 

    include MyAppModules::Publisher 

    after_create :publish_group_creation 

end