开发者

How to pass parameters to Rails "delegate"?

Currently I have this:

class Group < ActiveRecord::Base

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

end

The thing is that Publisher.publish_group_creation receives 1 argument (the group that's gonna be published)

I开发者_JAVA技巧 tried something like this;

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

but it doesn't work, what is right way to pass parameters using delegate ?


Methods called via callbacks don't receive parameters in Rails.

Try this:

class Group < ActiveRecord::Base
  after_create :publish_group

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


I'm not sure if this will work for your scenario, but you might try reworking this a bit. Something like

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

class Group < ActiveRecord::Base

  include MyAppModules::Publisher

  after_create :publish_group_creation

end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜