如何将参数传递给Rails“委托”?

时间:2009-11-24 17:04:09

标签: ruby-on-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传递参数的正确方法是什么?

2 个答案:

答案 0 :(得分:0)

我不确定这是否适用于您的场景,但您可能会尝试重新处理一下。像

这样的东西
module MyAppModules::Publisher
  def publish_group_creation
    ...
  end
end

class Group < ActiveRecord::Base

  include MyAppModules::Publisher

  after_create :publish_group_creation

end

答案 1 :(得分:0)

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

试试这个:

class Group < ActiveRecord::Base
  after_create :publish_group

  private
  def publish_group
    MyAppModules::Publisher.publish_group_creation(self)
  end
end
相关问题