1

我有一个类 GoodsTransaction:

require Rails.root.join('lib','two_parties_transaction.rb')

class GoodsTransaction < Transaction

  include TwoPartiesTransaction

  def counterparty(org, loop = false)

    res = #Some logic
    res || super(org)
  end
end

其中包括 TwoPartiesTransaction 模块:

module TwoPartiesTransaction
  extend ActiveSupport::Concern

  included do
    def counterparty(org)
       #Some logic
    end
  end
end

并且运行交易对手失败:

muichkine@ubuntu:~/src/white$ rails c
Faraday::Builder is now Faraday::RackBuilder.
Loading development environment (Rails 4.0.0)
2.0.0p247 :001 > tr = GoodsTransaction.new
=> #<GoodsTransaction _id: 5264e2eb75627540dd000000, e(end_date): nil, p(span): nil, s(start_date): nil, u(span_unit): nil, _type: "GoodsTransaction", a(autobook): nil, c(forced_close): nil, d(public_id): nil, k(autobook_period): nil, l(label): nil, n(autobook_number): nil, t(autobook_until): nil, contact_ids: nil, parent_id: nil, fc(creditor): nil, fd(debtor): nil> 
2.0.0p247 :002 > tr.counterparty(nil)
NoMethodError: super: no superclass method `counterparty' for   #<GoodsTransaction:0x000000090fe1d8>
from /home/muichkine/src/white/app/models/transactions/goods_transaction.rb:148:in `counterparty'

我在做什么跳过超级 coutnerparty 方法?

4

1 回答 1

1

要将模块中的实例方法包含到类中,您不需要使用ActiveSupport::Concern. 你可以做

module TwoPartiesTransaction
  def counterparty(org)
     # ...
  end
end

如果出于其他原因需要它,您仍然可以使用ActiveSupport::Concern它,即在类级别(例如scope等)执行代码或将类方法添加到包含类。有关详细信息ActiveSupport::Concern请参阅 API 文档

于 2013-10-21T09:17:56.117 回答