[英]Neo4j rb inheritance
我在Rails应用程序中使用Neo4j.rb。 我在多态性方面遇到麻烦。
我有这样的东西,我有一个Users类,看起来像这样:
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,它如何在数据库中发生,我是否还需要为该图像提供一个节点,或者可以一个普通的物体。
即
class Image < SocialMediaContent
include Neo4j::ActiveNode #Do i have to do this?
我想要这样的行为:每个用户都有很多SocialMediaContent,它们可以是图像或视频(目前),我现在不想指定,其中哪个是图像,哪个是视频。
谁能建议我如何做到这一点? 最后我可以存储一组SocialMediaContent而不是has_many关联(哪个更好?)
是的,您绝对可以做到这一点。 在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
,它将创建一个同时具有Image
和SocialMediaContent
标签的节点。 如果您使用Image.find
, Image.where
等搜索图像, Image.find
仅限于带有两个标签的节点。
至于关联,您应该能够按照问题中的说明进行指定(尽管如果没有type
, rel_class
或origin
选项,它会发出抱怨)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.