代码之家  ›  专栏  ›  技术社区  ›  jpemberthy

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

  •  1
  • jpemberthy  · 技术社区  · 16 年前

    目前我有:

    class Group < ActiveRecord::Base
    
       delegate :publish_group_creation, :to => 'MyAppModules::Publisher'  
       after_create :publish_group_creation
    
    end
    

    问题是 Publisher.publish_group_creation

    我试过这样的东西;

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

    delegate

    2 回复  |  直到 16 年前
        1
  •  0
  •   PreciousBodilyFluids    16 年前

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

    试试这个:

    class Group < ActiveRecord::Base
      after_create :publish_group
    
      private
      def publish_group
        MyAppModules::Publisher.publish_group_creation(self)
      end
    end
    
        2
  •  0
  •   Mike    16 年前

    我不确定这是否适用于您的场景,但您可以尝试重新设计一下。差不多

    module MyAppModules::Publisher
      def publish_group_creation
        ...
      end
    end
    
    class Group < ActiveRecord::Base
    
      include MyAppModules::Publisher
    
      after_create :publish_group_creation
    
    end