Undefined method error for association

52 views
Skip to first unread message

Jan

unread,
Nov 8, 2015, 2:10:15 PM11/8/15
to neo4jrb
Hi,

I have an existing Neo4J Database, which I have connected to a RoR Application. Since the db is already seeded I had to generate the migration file to add the uuids for the nodes, this worked. I have also existing relationships between those nodes (some of them have properties), e.g.: (u:User)-[:owns {property: 'value'}]->(p:Post) Currently I ignore the properties, and have added the owns-association to the User Model like this:

has_many :out, :posts, type: :owns

And to the Post model like this:

has_many :in, :users, origin: :users

In the Rails console User.first or Post.first work, however when I enter something like User.first.owns I get: NoMethodError: undefined method `owns' for #<User:0x007fdd869b8aa0>

Do I have to migrate somehow the relationships / associations? Thank you in advance for your responses!

Chris Grigg

unread,
Nov 8, 2015, 2:50:55 PM11/8/15
to neo...@googlegroups.com
Hi Jan,

The accessor methods are generated based on the second argument, not the `type` of the relationship. In your example you'd do `User.first.posts` or `Post.first.users`.

Let us know if you have any trouble!

Best,

Chris
--
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.

Jan

unread,
Nov 9, 2015, 5:17:08 AM11/9/15
to neo4jrb
Hi Chris,

Thank you very much for your answer. User.first.posts worked! I currently follow the Screencasts by Brian Underwood (thanks!) and the documentation on http://neo4jrb.readthedocs.org/en/5.2.x/ (thanks!)

And I am also already able to query this relationship with for example: User.first.posts.rel_where(property: value)

Follow up question: Should I implement the owns relationship and its properties in an extra class? I've created a Model `owns` with rails g model owns --force-plural (maybe --force-plural is a bad idea?) and have added to that file:

class Owns
  include Neo4j::ActiveRel
 
  from_class User
  to_class Post
  type :owns

  property :kind, type: Integer
  property :quality, type: Float
  property :weight, type: Float
end

I updated the User class to:

has_many :out, :concepts, rel_class: :owns

And I have left the Post class untouched.

has_many :in, :users, origin: :users

This ends in an "wrong constant name owns" error.

For my application I only need to read from the database. So I don't know if I need this extra class for the relationship? My ultimate / final goal is to replace this jQuery AJAX Request and to make the resulting JSON file available for each Author (=User) view (i.e. not only to Jack):

var query = {"statements": [{
  "statement": "MATCH (u:User {author: 'Jack'})-[r:owns]->(p:Post) RETURN r, p",
  "resultDataContents": ["graph"]
}]};

$.ajax({
  type:        "POST",
  accept:      "application/json",
  contentType: "application/json; charset=utf-8",
  data:        JSON.stringify(query),
  success:
    function(data, textStatus, jqXHR) {
      console.log(data)
    },
  failure:
    function(msg) {
      console.log("failed")
    }
});

Thanks again for your answer in advance! 

-Jan


Am Sonntag, 8. November 2015 20:50:55 UTC+1 schrieb Chris Grigg:
Hi Jan,

The accessor methods are generated based on the second argument, not the `type` of the relationship. In your example you'd do `User.first.posts` or `Post.first.users`.

Let us know if you have any trouble!

Best,

Chris

On Sunday, November 8, 2015, Jan <zero...@gmail.com> wrote:
Hi,

I have an existing Neo4J Database, which I have connected to a RoR Application. Since the db is already seeded I had to generate the migration file to add the uuids for the nodes, this worked. I have also existing relationships between those nodes (some of them have properties), e.g.: (u:User)-[:owns {property: 'value'}]->(p:Post) Currently I ignore the properties, and have added the owns-association to the User Model like this:

has_many :out, :posts, type: :owns

And to the Post model like this:

has_many :in, :users, origin: :users

In the Rails console User.first or Post.first work, however when I enter something like User.first.owns I get: NoMethodError: undefined method `owns' for #<User:0x007fdd869b8aa0>

Do I have to migrate somehow the relationships / associations? Thank you in advance for your responses!

--
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+unsubscribe@googlegroups.com.

Brian Underwood

unread,
Nov 9, 2015, 8:50:24 AM11/9/15
to neo...@googlegroups.com
The `rel_class` needs to match case-sensitive to your `ActiveRel` class name.  In your case your `o` needs to be capitalized like this:

has_many :out, :concepts, rel_class: :Owns

`ActiveRel` models certainly aren't required.  They start to come in useful when you want properties, validations, and callbacks on relationships.  You have properties on your relationships, but if you don't define an `ActiveRel` class you'll still get back a wrapped relationship object from `neo4j-core` which will have a `props` method to get you your properties.  If you want to save properties / make sure they're of the right type, then you'll start wanting to use `ActiveRel`.

Brian

Chris
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.

--
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.

Jan

unread,
Nov 10, 2015, 7:15:25 AM11/10/15
to neo4jrb
Hi Brian,

Thank you very much for pointing that out with the capitalization. I didn't see that. Now everything works as expected. As already assumed, I do not need the extra class for my use case, but it is nice to know how things work, so I really appreciate your explanation.

I would now like to transform the jQuery AJAX request from my previous post. I have specified that I get an array of GraphObjects back by stating:

"resultDataContents": ["graph"]

I am wondering, what the correct approach is, to do that with neo4j.rb? Following your Screencasts Series I formulated a Query like:

@user.posts.each_with_rel

I have put this into my users controller like this:

...
@query = @user.posts.each_with_rel
    respond_to do |format|
      format.html
      format.json { render json: @query }
    end
...

and log it to the browser console from my html view like:

<%= javascript_tag do %>
  query = '<%= @query %>'
$.getJSON(query + '.json', function(data) {
  console.log(data);
});
<% end %>

This produces a similar looking array (only endnode (post) and relationship (owns)). I am wondering, if I can get a JSON file which looks exactly the same using neo4j.rb?

Again, thanks in advance for any responses!
-Jan

Chris
To unsubscribe from this group and stop receiving emails from it, send an email to neo4jrb+unsubscribe@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.

Brian Underwood

unread,
Nov 11, 2015, 5:49:23 AM11/11/15
to neo...@googlegroups.com
For the JSON I think you would need to collect together all of the nodes and relationships yourself and build a Hash which you could call `to_json` on.  There's nothing built into neo4j.rb, though I have been thinking about the idea of allowing users to get back raw responses for queries without being wrapped so you could have the convenience of building the query and the control over the response format that you get back.

Brian
;p

Chris
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.

--
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.

Jan

unread,
Nov 16, 2015, 5:25:58 AM11/16/15
to neo4jrb
Hi Brian

Thank you for your answer. Sorry, I was quite busy at work last week, so I did not find the time to just say Thanks for the answer. I will try to rebuild the answer from the jQuery AJAX call by myself. Maybe raw responses could be helpful sometimes, however, I am currently not familiar enough with all the power neo4j.rb has to offer.

Thanks again for the help (also to Chris). Have a great week.
-Jan
Chris
To unsubscribe from this group and stop receiving emails from it, send an email to neo4jrb+unsubscribe@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.

--
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.

Brian Underwood

unread,
Nov 20, 2015, 4:02:08 AM11/20/15
to neo...@googlegroups.com
Good luck!  If you need help we (and others) hang around on Gitter:


And the Neo4j slack channel:


Brian

Chris
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.

--
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.

--
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.
Reply all
Reply to author
Forward
0 new messages