3

使用 Rails 4、Zeus 0.13.3、rspec-rails edge 和 mongoid edge

简单的 STI 模型:

目录my_model.rb中的文件/model

class MyModel
  include Mongoid::Document

  field :my_field
end

目录my_sti.rb中的文件/model/my_model

class MyModel
  class MySti << ::MyModel

    field :some_other_field
  end
end 

一切都在控制台和 Web 中按预期工作。当我通过捆绑器运行规范时:

bundle exec rspec 规范

一切都很好,但问题是当我用 运行它们时zeus,它会抛出:

<class:MySti>': undefined methodMyModel::MySti:Class (NoMethodError) 的字段'

解决我迄今为止发现的问题的两种方法:

  • 评论fieldSTI 中的声明,规范运行良好,但显然业务逻辑不是!

  • 在类中重新添加include Mongoid::Document定义MySti:规范和逻辑可以正常工作,但我不应该这样做,而且我厌倦了这样做可能带来的意外后果。

有任何想法吗 ?

4

1 回答 1

0

添加委托有效吗?

从文档

class Greeter < ActiveRecord::Base
  def hello
    'hello'
  end

  def goodbye
    'goodbye'
  end
end

class Foo < ActiveRecord::Base
  belongs_to :greeter
  delegate :hello, to: :greeter
end

Foo.new.hello   # => "hello"
Foo.new.goodbye # => NoMethodError: 
于 2014-04-17T20:31:02.127 回答