Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove relationship in Neo4jRB 3.0?

I have two models:

class Topic 
  include Neo4j::ActiveNode
  has_many :in, :favorited_by, model_class: User, origin: :favorite_topics
end

and

class User 
  include Neo4j::ActiveNode
  has_many :out, :favorite_topics, model_class: Topic, type: 'favorited_by'
end

How I can remove only association?

irb(main):008:0> Topic.first.favorited_by.delete(User.first)
NoMethodError: undefined method `delete' for #<Neo4j::ActiveNode::Query::QueryProxy:0x00000004b27f10>

Thank you.

like image 432
pavels Avatar asked Dec 08 '14 09:12

pavels


2 Answers

Sorry for my first answer, I thought it was a has_many assocation. This works, but it's not ideal:

topic = Topic.first
user = User.first
topic.favorited_by = topic.favorited_by.to_a - [user]

EDIT:

topic.favorited_by(:user, :rel).match_to(user).delete_all(:rel)

This is a bit better, but still not great. I just created a github issue for this:

https://github.com/neo4jrb/neo4j/issues/630

like image 61
Brian Underwood Avatar answered Nov 15 '22 00:11

Brian Underwood


Piggybacking on Brian's comment, you can do Topic.first.favorited_by.first_rel_to(User.delete).destroy if you're running the latest release, 4.0.0.rc.1, and know that you only have one relationship between the two.

If you're using the master branch from Github, I just added delete and destroy methods to QueryProxy. Topic.first.favorited_by.delete(User.first) will run from the database, Topic.first.favorited_by.destroy(User.first) will return the relationship to Ruby and call delete, triggering callbacks. These will be will in the next release, which should be soon.

like image 23
subvertallchris Avatar answered Nov 15 '22 00:11

subvertallchris