Neo4jrb:model_class的枚举器:false

时间:2016-06-10 14:18:59

标签: ruby-on-rails neo4j.rb

指定model_class false时,预计从此节点到另一个节点的连接可以是任何其他类节点。

是否有一个互惠的枚举器,以便我们可以调用类似的东西:

ActiveNodes.all

为用户提供可供选择的节点选择?

注意(并警告任何想到这一点的人):寻找所有节点都不会扩展

1 个答案:

答案 0 :(得分:1)

如果您有一个好的用例我们肯定会考虑让它变得更容易。但问题是,那时没有关联浏览,因为没有涉及模型。您可能只想在此时使用Query API:

Neo4j::Session.current.query.match(:n).where(n: {foo: 'bar'}).etc...

所有方法的文档都在这里:

http://neo4jrb.readthedocs.io/en/7.0.x/QueryClauseMethods.html

修改

根据您的评论修改帖子,因为评论中没有足够的空间;)

您的示例是您有一个Product,并且您希望能够找到与相同关系类型链接的其他模型。这就是我可能做的事情:

class Product
  include Neo4j::ActiveNode

  has_one :out, :file, type: :RELATED_FILE, model_class: %i(ProductImage ProductVideo ProductPDF)

 # optionally
 has_one :out, :image_file, type: :RELATED_FILE, model_class: :ProductImage
 has_one :out, :video_file, type: :RELATED_FILE, model_class: :ProductVideo
 has_one :out, :pdf_file, type: :RELATED_FILE, model_class: :ProductPDF
end

这些也可以更改为has_many,如果有多个关联的可能性,也可以正常工作。

然后你应该能够在视图中生成一个包含所有选项的选择标记,并通过参数将ID传递给控制器​​,然后你可以这样做:

Product.create(name: 'or whatever props you have', file_id: params[:file_id])

如果关联是has_many,您可以改为file_ids: params[:file_ids]

您也可以考虑从单个类继承这些关联模型,如下所示:

class File
  include Neo4j::ActiveNode
end

class ProductImage < File
  # No need to `include Neo4j::ActiveNode`
end

然后你可以像上面我建议的那样做一系列模型,或者你可以做:

class Product
  include Neo4j::ActiveNode

  has_one :out, :file, type: :RELATED_FILE
end
根据关联的名称假设

model_class: :File。如果您有has_many,那么您的关联名称将为:files,并且仍会假定为model_class: :File

请注意,如果您这样做,相关模型的节点(ProductImageProductVideoProductPDF)都会有File标签,因为继承。

作为一个单独的建模建议,我可能只有Image模型(例如)而不是ProductImage,因此模型可能会被重复使用。