ruby-on-railsneo4j.rb

Neo4j.rb How to merge two realtionship between node


class User
  include Neo4j::ActiveNode

  has_many :in, :homes, origin: :owner
  has_many :in, :shared_homes, origin: :home_mates, model_class: 'Home'
end

class Home
  include Neo4j::ActiveNode

  has_many :out, :housemates, type: :home_mate, model_class: 'User'
  has_one :out, :owner, type: :owner_of, model_class: 'User'
end


# f_ids is array of users
@users = User.as(:c).where(uuid: f_ids)

# for homes
@homes = @users.homes(:p)

# for shared homes
@homes = @users.shared_homes(:p)

I want to merge these two records like both type of places will be refrenced in :p

Any solution I was also trying to do that in neo4j.rb before going to cypher


Solution

  • I would really like to allow the type option of has_one / has_many to support an array of relationship types so that you could do something like:

    has_many :in, :associated_homes, type: [:home_mates, :owner_of]
    

    That's not possible yet, unfortunately, but you can get by for now this hack to create such as association:

    has_many :in, :associated_homes, type: 'home_mates`|`owner_of'
    

    Protip: User.as(:c).where(uuid: f_ids) could simply be User.where(id: f_ids)