【问题标题】:Neo4jrb: undefined method 'each' for nil:NilClassNeo4jrb:nil:NilClass 的未定义方法“每个”
【发布时间】:2016-03-24 19:56:55
【问题描述】:

当创建两个类以及它们之间的关系时;

(粗略的例子)

class User
  include Neo4j::ActiveNode
  property :name, type: String
  property :email, type: String
end

class Group
  include Neo4j::ActiveNode
  property :name, type: String
  property :email, type: String

  has_many :in, :users, type:BELONGS_TO, model_class: :User
end

如果该组中有用户(现有的节点-边缘关系),则调用 @group.users.each ... 有效,但如果没有,Rails 将失败并显示 undefined method 'each' for nil:NilClass

当没有连接的节点时,最高效的优雅失败方式是什么?

【问题讨论】:

  • 这不应该发生,它应该表现得像一个空的可枚举。你能检查你的代码是否有错别字吗?我可以在您提供的代码示例中看到一个,type:BELONGS_TO :,尽管这会产生一个非常不同的错误......不过,再给它一次。
  • 我会给你一个更好的。我已经从rails new 直接转到有问题的项目。 github.com/Joshfindit/…
  • 复习一下,我注意到在试图简化问题时,我说错了一些东西。我正在尝试调用@group.users.each NOT Group.users.each(遵循 Rails 在生成脚手架视图时使用的内容)

标签: ruby-on-rails neo4j


【解决方案1】:

我想听听您从 Chris 的建议中发现的内容,但在链中添加 all 可能会有所帮助:

Group.all.users.each...

【讨论】:

    猜你喜欢
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 2015-07-02
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多