1

我在我的 rails 应用程序中使用 Neo4j.rb。我在多态性方面遇到了麻烦。

我有这样的东西我有一个看起来像这样的用户类:

class User 
  include Neo4j::ActiveNode 

  #other properties

  has_many: out, :social_media_content, model_class: :SocialMediaContent
end

我有 SocialMediaContent 类

class SocialMediaContent   
  include Neo4j::ActiveNode # Is it necessary?

  property :first_name, type: Integer
  property :first_name, type: Integer

end 

我想要一个继承自社交媒体内容的图像类和视频类

这是否意味着在创建 SocialMediaContent 和用户之间的关系时,我可以提供图像或视频对象而不是 SocialMediaContent,它是如何在数据库中发生的,我是否也需要为图像创建一个节点,或者它可以是一个普通的物体。

IE

class Image < SocialMediaContent
  include Neo4j::ActiveNode #Do i have to do this?

我想要这样的行为:每个用户都有许多 SocialMediaContent 可以是图像或视频(现在)我现在不想指定,其中一个是图像,哪个是视频。

谁能建议我如何实现这一目标?最后我可以存储一个 SocialMediaContent 数组而不是 has_many 关联(哪个更好?)

4

1 回答 1

2

是的,你绝对可以做到这一点。在 Neo4j.rb 中,当您从一个类继承一个类时ActiveNode,子类表示具有两个标签的节点。例如:

class SocialMediaContent
  include Neo4j::ActiveNode # This is necessary for the parent
end

class Image < SocialMediaContent
  # No include needed
end

class Video < SocialMediaContent
  # No include needed
end

现在,如果您这样做Image.create,它将创建一个同时具有ImageSocialMediaContent标签的节点。如果您搜索带有 、 等的图像Image.findImage.where它将仅限于具有两个标签的节点。

至于关联,您应该能够在问题中指定它(尽管如果您没有 、 或 选项,它会type抱怨rel_classorigin

于 2015-10-19T13:56:45.987 回答