简体   繁体   English

Neo4j gem-销毁关联节点和关系的有效方法

[英]Neo4j gem - Effecient ways to destroy associated nodes and relationships

I know that the gem doesn't have a built in way to handle dependency destroys yet like active rececord dependant: destroy 我知道gem没有内置的方式来处理依赖项销毁,但就像主动rececord dependant: destroy

Is there better ways to deal with chain destroying? 有没有更好的方法来处理连锁破坏?

For example, right now I am trying to deal with destroying with my Q+A 例如,现在我正在尝试用我的Q + A处理破坏

Each question has many answers and each answer has one question 每个问题有很多答案,每个答案有一个问题

Destroying the question can be done like so 可以这样破坏问题

event_question = EventQuestion.find(params[:id])

event_question.destroy

but if I need to deal with the answers, and I do it through this association, I have to loop through and destroy each one. 但是如果我需要处理答案,并且通过这种关联来做到这一点,就必须遍历并销毁每个答案。 An added weakness is needing to check if the dependent nodes/relationships even exist. 另一个缺点是需要检查从属节点/关系是否存在。

Is it better to write out a longer query to deal with finding both the questions and answers and destroying them in one swoop? 编写更长的查询以同时查找问题和答案并一口气销毁它们是否更好? (or... can you?) I know you can't destroy the queryproxy like so (eg event_question.answers.destroy ) (或...可以吗?)我知道您无法像这样销毁queryproxy(例如event_question.answers.destroy

UPDATE 更新

I've tried to implement these two queries with delete but delete doesn't execute. 我试图用delete实现这两个查询,但是delete不执行。 No error message right now. 现在没有错误消息。 Prying into the method, the matches look right. 探究该方法,匹配看起来正确。

event.users(:u,:rel).query.match("()-[r3]-u").delete(:r3).exec
event.event_questions(:q).event_answers(:a).query.match("event-[r0]-(), q-[r1]-(), a-[r2]-()").delete(:q, :a, :event).exec

UPDATE 2 更新2

This is the cypher query for the first event.users . 这是对第一个event.users的密码查询。

MATCH (event13:`Event`), (u:`User`), event13-[rel:`invited`]-(u:`User`), ()-[r3]-u WHERE ID(event13) = {ID_event13} DELETE r3"

It looks right? 看起来对不对? But delete doesn't seem to execute. 但是删除似乎没有执行。 Attaching as or query_as doesn't allow the query to go through on the second one. 附加asquery_as不允许查询通过第二个查询。 It gives me undefined method as 它给了我未定义的方法as

LAST UPDATE 最后更新

Last update before moving to a different question. 移至其他问题之前的最新更新。

Oddly, I can get the first query to delete the relationship via this 奇怪的是,我可以通过此操作获取第一个查询以删除关系

event.users(:u,:r3).query.match("()-[r]-u").delete(:r3).exec

but this next query won't delete the question and answers. 但是下一个查询不会删除问题和答案。

event.event_questions(:q).event_answers(:a).query.match("q-[r1]-(), a-[r2]-()").delete(:q, :a).exec

I'm not entirely sure why the match is needed but without it, it doesn't execute either. 我不确定为什么需要匹配,但是没有匹配,它也不执行。 When I wrote my destroy functions on my Questions/Answers controller, I didn't actually have to delete the relationship. 当我在Questions / Answers控制器上编写destroy函数时,实际上并不需要删除该关系。 But maybe I did something funky. 但是也许我做了一些时髦的事。

I actually did this 我实际上是这样做的

answers = event_question.event_answers

and then looped it and destroyed each answer, which seemed to destroy both answer and the relationship with the question. 然后循环播放并破坏每个答案,这似乎破坏了答案以及与问题的关系。 The Q+A are set up through associations and not ActiveRel.. not sure if that makes a difference. Q + A是通过关联设置的,而不是通过ActiveRel ..不确定是否有所不同。

The gem implements callbacks with ActiveModel. gem使用ActiveModel实现回调。 So you can do: 因此,您可以执行以下操作:

class EventQuestion
  before_destroy :destroy_answers

  def destroy_answers
    answers.each(&:destroy)
  end
end

Obviously that's not the most efficient as it does a destroy for each answer which is it's own query. 显然,这不是最有效的方法,因为它会对每个答案进行销毁,这是它自己的查询。 It does have the advantage that all of the before/after destroy callbacks on the answers will be called, though. 这样做的好处是,答案中的所有before / after destroy回调都会被调用。

If you wanted to just delete them with cypher you should be able to do answers(:a, :r).delete(:a, :r).exec 如果您只想使用密码删除它们,则应该可以执行answers(:a, :r).delete(:a, :r).exec

Chris and company is working on a PR for the dependent destroy ability. 克里斯和公司正在为dependent destroy能力进行PR。 It seems to work on master branch of the gem currently. 目前似乎可以在gem的master分支上使用。 It seems to do the job a -ok! 看起来做得不错-好!

https://github.com/neo4jrb/neo4j/pull/653 https://github.com/neo4jrb/neo4j/pull/653

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM