1

I am using Neo4j.rb in my rails app. I am having trouble with polymorphism.

I have something like this I have a Users class which looks like this:

class User 
  include Neo4j::ActiveNode 

  #other properties

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

I have the SocialMediaContent class

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

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

end 

I want to have an image class and a video class which inherit from Social media content

Does this mean that while creating a relationship between SocialMediaContent and a User i can provide an image or a video object instead of SocialMediaContent, How does it happen in the database, do i need to have a node for the image as well or can it be an ordinary object.

i.e

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

I want a behaviour like this: every User has many SocialMediaContent which can be either an image or a video (for now) i do not want to specify now, which one of them is an image and which is a video.

Can anyone suggest how i might acheieve this? And finally can i store an array of SocialMediaContent instead of has_many associations (which is better?)

Artemis Fowl
  • 301
  • 1
  • 10

1 Answers1

2

Yes, you can definitely do this. In Neo4j.rb when you inherit a class from an ActiveNode class then the child class represents nodes with both labels. For example:

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

Now if you do Image.create it will create a node which has both the Image and SocialMediaContent labels. If you search for an image with Image.find, Image.where, etc... it will limit to only nodes with both labels.

As for the association, you should be able to specify it as you have it in the question (though it's going to complain if you don't have a type, rel_class, or origin option).

Brian Underwood
  • 10,746
  • 1
  • 22
  • 34