Trying to auto create relationship and related nodes upon creating in rails app

67 views
Skip to first unread message

Ryan LeViseur

unread,
Oct 4, 2014, 3:30:18 PM10/4/14
to neo...@googlegroups.com
Hey guys, I've been playing with getting neo4j working with a basic sample rails app, but am having issues. I've gone through nearly every example app and guide I can find, but they all deal with creating nodes and relationships separately and manually. So here's what I'm trying to accomplish.

I was trying to create a web form (in a Rails app) to enter details about a person. One of those details would be their gender. Upon submitting the form, Rails would create a new node for that person with properties for each of the details except for their gender. It would also create a new “GENDER” relationship between that person’s node and the appropriate node for their gender (either male or female). 

The form is currently creating the person node just fine, but I can't seem to figure out the gender piece. I am running neo4j 2.1.3, neo4j-core 3.0.0, neo4j 3.0.1, rails 4.1.5 and Ruby 2.1.2. To make things easier for people to help, I've pushed the code to github: https://github.com/vidkun/genie-neo4j

Any insight or help on what I am doing wrong would be greatly appreciated. Thanks!





Brian Underwood

unread,
Oct 5, 2014, 2:53:14 AM10/5/14
to neo...@googlegroups.com
We actually have an example of something similar in our new intro screencast:

https://www.youtube.com/watch?v=bDjbqRL9HcM&list=UUIURLB8eZjFU8AH-GKeQI6g

It would work for you assuming that you want to have a specific list
of genders. The basic idea is that you would create a drop-down in
your form which lists all of the nodes and their IDs, you would pass
that ID from the form to the controller as "gender" property, and then
ActiveNode would find the node with that ID and make a relationship.
Example code:

person.update(first_name: 'Brian', gender: <ID string representing the
gender node>)

Or alternatively I think you could do:

person.gender = <ID string representing the gender node>
person.save

If you want to allow for free-form text entry (I saw a great
presentation on why you might do this. Can't seem to find it now):

gender = Gender.query_as(:gender).merge(gender: {name: 'Whatever
String'}).pluck(:gender).first
person.update(first_name: 'Brian', gender: gender)

I think that will work. I'm also realizing it's not ideal and we
should probably have some find_or_create type higher level
functionality (here I'm using query_as to get to the Cypher generation
API in neo4j-core).

Brian
;p
> --
> You received this message because you are subscribed to the Google Groups
> "neo4jrb" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to neo4jrb+u...@googlegroups.com.
> To post to this group, send email to neo...@googlegroups.com.
> Visit this group at http://groups.google.com/group/neo4jrb.
> For more options, visit https://groups.google.com/d/optout.

Ryan LeViseur

unread,
Oct 5, 2014, 6:09:32 PM10/5/14
to neo...@googlegroups.com
Hey Brian,

Thanks for the response. I did look at the screencast before posting my question. That example relies on the gender nodes to already be created when adding the person. Technically that will work and isn't that difficult, but with this example I was more trying to learn and figure out the concept of being able to dynamically create the node if it doesn't exist and then build the relationship upon creating the person. Mostly because it will be helpful in future features. 

For example, if I later wanted to add the ability to add different types of media to people. I could upload an image that has multiple people. This can be done on the first person which would create the new node with the image and then additional people can later be related to it as well. 

I don't know. Maybe I'm really over thinking all of this, but I'm trying grasp the way I need to change up my typical rails & active record work flows in order to really use the power of neo4j for a genealogy app I've been wanting to build.

I'll take another stab at it later tonight, though.

Also, am I doing something wrong or does accepts_nested_attributes_for not work with the 3.0.1 release?

Thanks again,

Ryan

Brian Underwood

unread,
Oct 6, 2014, 7:27:43 AM10/6/14
to neo...@googlegroups.com
Not sure if you saw the end of my last E-Mail, but I believe it covers
a way to build a node and then link the relationship. It's two lines,
which isn't ideal. I just created this issue for discussion:
https://github.com/neo4jrb/neo4j/issues/510

And accepts_nested_attributes_for doesn't work as far as I know. Did
it work in a previous release? Feel free to create an issue if you
need it

Brian
;p

Ryan LeViseur

unread,
Oct 6, 2014, 10:44:05 AM10/6/14
to neo...@googlegroups.com
I saw it, just haven't had a chance to go back and try testing it out yet. Thanks, though.

As for accepts_nested_attributes_for, I haven't ever had it working. I just asked because I saw it mentioned in the docs for the previous 2.x releases and it was something I'm familiar with in regular ActiveRecord for achieving something similar to what I was doing here. Basically, nested forms.

Thanks again and I'll keep an eye on that github issue.

-Ryan


On Saturday, October 4, 2014 1:30:18 PM UTC-6, Ryan LeViseur wrote:
Reply all
Reply to author
Forward
0 new messages