未定义的方法'detect':“Model#property”:符号。自我参照Neo4j模型定义不好?

时间:2012-07-03 07:24:45

标签: ruby-on-rails ruby-on-rails-3.1 jruby neo4j

运行单元测试时,我的一些测试会出现以下错误:

     6) Error:
test_name_can't_be_nil(NodeTest):
NoMethodError: undefined method `detect' for :"Node#children":Symbol
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/naming.rb:85:in `model_name'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/errors.rb:315:in `generate_message'
    org/jruby/RubyArray.java:2339:in `collect'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/errors.rb:314:in `generate_message'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/errors.rb:235:in `add'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/errors.rb:256:in `add_on_blank'
    org/jruby/RubyArray.java:1615:in `each'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/errors.rb:254:in `add_on_blank'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/validations/presence.rb:9:in `validate'
    org/jruby/RubyBasicObject.java:1704:in `__send__'
    org/jruby/RubyKernel.java:2101:in `send'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activesupport-3.1.4/lib/active_support/callbacks.rb:306:in `_callback_before_47'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activesupport-3.1.4/lib/active_support/callbacks.rb:410:in `_run_validate_callbacks'
    org/jruby/RubyBasicObject.java:1698:in `__send__'
    org/jruby/RubyKernel.java:2097:in `send'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activesupport-3.1.4/lib/active_support/callbacks.rb:81:in `run_callbacks'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/validations.rb:212:in `run_validations!'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/validations.rb:179:in `valid?'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/neo4j-2.0.1-java/lib/neo4j/rails/validations.rb:29:in `valid?'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/neo4j-2.0.1-java/lib/neo4j/rails/callbacks.rb:26:in `valid_with_callbacks?'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activesupport-3.1.4/lib/active_support/callbacks.rb:405:in `_run_validation_callbacks'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/neo4j-2.0.1-java/lib/neo4j/rails/callbacks.rb:26:in `valid_with_callbacks?'
    /Users/dave/Projects/myapp/test/test_helper.rb:11:in `assert_presence'
    /Users/dave/Projects/myapp/test/unit/node_test.rb:9:in `test_name_can't_be_nil'
    org/jruby/RubyBasicObject.java:1698:in `__send__'

有问题的违规测试是:

class NodeTest < ActiveSupport::TestCase
    def assert_presence( model, field)
        model.send( (field.to_s + '='), nil )
        model.valid?
        assert_match /can not be blank/, model.errors[field].join,
        "Presence error for #{field} not found on #model.class"
    end
    def setup
        @node = FactoryGirl.build :node
    end

    test "name can't be nil" do
        assert_presence @node, :name
    end
    ....
end

其中节点在模型中定义为:

class Node < Neo4j::Rails::Model
    include Neo4j::NodeMixin

    property :name, type: String, index: :exact
    property :description, type: String

    has_one(:creator).from(User, :created_nodes)

    has_n(:children).to(Node)              ## <--- Suspected difficult line
    has_n(:parents).from(Node, :children)  ## <--- Suspected difficult line
    has_n(:capable_users).from(User, :capabilities)

    # The following has the Class commented out because this somehow triggers
    # an error from the 'has_n(:children).to(Node)' line
    has_n(:challenges)  #.to(Challenge)
    ....
end

基本上,服务器可以正常运行,但在我尝试调用有效时会崩溃吗?在节点上(不是其他型号)。有谁知道为什么会发生这种情况?

1 个答案:

答案 0 :(得分:0)

问题在于我选择的属性,即父母(也许是孩子)。

我查看了发生错误的活动模型中的源代码(here),并看到了这一点:

def model_name
  @_model_name ||= begin
    namespace = self.parents.detect { |n| n.respond_to?(:_railtie) }  # <--- this line
    ActiveModel::Name.new(self, namespace)
  end
end

如您所见,父母似乎正在使用中。我更改了名称和验证,保存等,现在有效。