ruby-on-railsrubyneo4jneo4j.rb

Neo4j.rb How to Subclass ActiveRel relationship?


Newbie Neo4j.rb (v8.2.1) gem user here trying to figure out how to handle subclassing ActiveRel properly...

I have one relationship type called HasAccount that I then subclass in others, i.e. OwnsAccount < HasAccount. I have relationships defined in my model like this:

has_many :out, :accounts, rel_class: :HasAccount
has_many :out, :owned_accounts, rel_class: :OwnsAccount
has_many :out, :managed_accounts, rel_class: :ManagesAccount

My intent is that whenever I create an OwnsAccount or ManagesAccount relationship for a node, they'd also be accessible via my_node.accounts (i.e. the superclass' has_many).

Am I approaching this wrong? I've tried every which way and that shy of ditching HasAccount entirely and definining an accounts method that merges owned_accounts and managed_accounts...


Solution

  • I don’t think that subclasses ActiveRel classes would help, unfortunately (probably you’ve found that ;) ).

    If you didn’t have an ActiveRel class you can specify multiple types (I don’t remember if we implemented type: [:OWNS_ACCOUNT, :MANAGES_ACCOUNT] but you could certainly do either type: "OWNS_ACCOUNT|MANAGES_ACCOUNT”’ or type: false / type: :any).

    If you need to have logic on your relationships then you would need ActiveRel. You can have type :any on the ActiveRel class I think, but I don’t know how you’d have separate owned_accounts / managed_accounts associations in that case (I think type and rel_class are mutually exclusive, but it might be work trying).

    Really, probably, we should allow for rel_class: [:OwnsAccount, :ManagesAccount], I think