{"action":"opened","issue":{"url":"https://api.github.com/repos/neo4jrb/neo4j/issues/840","labels_url":"https://api.github.com/repos/neo4jrb/neo4j/issues/840/labels{/name}","comments_url":"https://api.github.com/repos/neo4jrb/neo4j/issues/840/comments","events_url":"https://api.github.com/repos/neo4jrb/neo4j/issues/840/events","html_url":"https://github.com/neo4jrb/neo4j/issues/840","id":89590863,"number":840,"title":"[Discussion] Creating nodes and rels at the same time/without persisting nodes first","user":{"login":"olance","id":314079,"avatar_url":"https://avatars.githubusercontent.com/u/314079?v=3","gravatar_id":"","url":"https://api.github.com/users/olance","html_url":"https://github.com/olance","followers_url":"https://api.github.com/users/olance/followers","following_url":"https://api.github.com/users/olance/following{/other_user}","gists_url":"https://api.github.com/users/olance/gists{/gist_id}","starred_url":"https://api.github.com/users/olance/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/olance/subscriptions","organizations_url":"https://api.github.com/users/olance/orgs","repos_url":"https://api.github.com/users/olance/repos","events_url":"https://api.github.com/users/olance/events{/privacy}","received_events_url":"https://api.github.com/users/olance/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-06-19T15:22:18Z","updated_at":"2015-06-19T15:22:18Z","closed_at":null,"body":"So we've started this discussion on Gitter and many ideas came out...\r\n\r\nThe initial question was: could we make it possible to create relationships between two possibly transient nodes?\r\n\r\n@subvertallchris suggested a way to do this:\r\n\r\n> There would need to be a little magic. When setting the association, if either node is non-persisted, it’d need to store info about the association in one of the node’s association cache. save could look at the cache and find pending actions.\r\n\r\nWe could have a look at what's done in ActiveRecord and see if we can copy some behaviors from there.\r\n\r\nWe also talked about coming up with some kind of DSL to be able to create nodes and relationships much like Cypher natively allows us to do. Something like this could be nice?\r\n\r\n```ruby\r\nnode.query_as(:node) do\r\n  match '(node)-[rels*]->(n)'\r\n  where 'ALL (rel in rels WHERE rel.valid = 1)'\r\nend.return(:n)\r\n```\r\n\r\nI'm not fully convinced it would be more useful than what's already there, especially since we couldn't use `#return` without writing `self.return`, which kinda defeats the DSL-like construct.\r\n\r\nHowever we could already start by adding methods to QueryProxy?\r\nHere's an idea, using association's `type` value to create chaining methods to declare relationships on nodes:\r\n\r\n```ruby\r\nclass Student\r\n  include Neo4j::ActiveNode\r\n  property :name\r\n  has_many :out, :lessons, type: :enrolled_in, model_class: 'Lesson'\r\n  has_many :both, :friends, type: :has_friend, model_class: self\r\nend\r\n\r\nclass Lesson\r\n  include Neo4j::ActiveNode\r\n  property :title\r\n  has_many :out, :tags, type: :has_tag, model_class: 'Tag'\r\nend\r\n\r\nclass Tag\r\n  include Neo4j::ActiveNode\r\n  property :title\r\nend\r\n\r\n# Create and save nodes & relationships on the fly:\r\nlesson = Lesson.new(title: \"Learning Neo4j\")\r\ncheerfulstoic = Student.where(name: \"cheerfulstoic\").first\r\nolance = Student.new(name: 'olance').enrolled_in(lesson).has_friend(cheerfulstoic).save\r\n```\r\n\r\nWe could probably do more than that, but I guess that should then go at the neo4j-core level...\r\n\r\nI really can see the benefit of being able to create rels and nodes on the fly like above. We'd have to make some rules about the method names inferred from `type` when it is not explicitly given (using \"has_\" + `target_class_name`?).\r\n\r\nWhat do you think? ^^"}}