ActiveSupport ::关注缺少超级

时间:2013-10-21 09:11:29

标签: ruby-on-rails ruby activesupport

我有一个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方法?

1 个答案:

答案 0 :(得分:1)

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

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

如果由于其他原因需要,您仍然可以使用ActiveSupport::Concern,即在类级别执行代码(例如scope等)或将类方法添加到包含类。有关ActiveSupport::Concernsee the API documentation

的详细信息