1

I'm not exactly sure what's gone on inside my app but my queries don't seem to give me my results in order of creation. It recently rearranged itself without me changing the code related to the query. However, I've obviously done SOMETHING.

At one point I was trying to get my rails c results writen into a file. However, that was simply writing the output of the query to the file. I didn't think that would effect query orders...

The queries were simply like

File.open('text.txt', 'w') do |f|
   User.all.each do |u|
      f.puts u.email
   end
end

I think I can re-sort one of my relationships by adding property :created_at and sorting it by that timestamp.

But there is no way for me to do this with my Q+A association as they aren't activerel right now.

It's called like this

<% @event.event_questions.each do |q| %>

In the app there were 3 questions created. The order that it ended up showing was

2,1,3

How does the neo4j query determine the order of the objects? What could I have possibly done wrong?

Clam
  • 935
  • 1
  • 12
  • 24

1 Answers1

1

I don't think that neo4j has any way of determining the date/time of when nodes are created. It stores an internal, incrementally generated numeric ID, but those IDs can be recycled when you restart your server. It could be that order changed after you restarted your server. I'm not sure if neo4j returns nodes in any sort of order by default. I'm assuming it doesn't guarantee order unless you specify an ORDER BY

For the ordering of relationships without ActiveRel, you can do:

@event.event_questions(:question, :rel).order('rel.created_at')
Brian Underwood
  • 10,746
  • 1
  • 22
  • 34
  • 1
    This is correct. It's not even that objects are necessarily ordered by ID, I think it has something to do with the query plan that's used to find them. With Neo4j 2.2.0, the order of objects returned from a simple `MATCH (u:User)-[:KNOWS]->(other:User) RETURN other` will be in the opposite order of what you'd get from that same query in 2.1.x. – subvertallchris Feb 02 '15 at 15:11
  • So by the sounds of it, the only way I can fix this is that I'm forced to make my Q+A into ActiveRel rather than simple assocation – Clam Feb 02 '15 at 19:09
  • I don' think you need to use ActiveRel. Did you see my example in the answer? – Brian Underwood Feb 02 '15 at 19:30
  • No, I was working under the assumption that you've defined it because you need to have something, but the gem does consider `updated_at` and `created_at` as special (that is, it sets them as DateTimes automatically and updates them when appropriate, like ActiveRecord). So all you need to do is define `property :created_at` – Brian Underwood Feb 04 '15 at 12:19
  • the thing is though, even if i define this in my node e.g. `event_questions`, here won't be a `rel.created_at` . at least not one i can see when i fire up my visual db to look at the relationships – Clam Feb 05 '15 at 08:34
  • Ah, yes, good point. For that you'd need ActiveRel. You can define an association with ActiveRel to model the relationship, but I don't know what defining a `created_at` property on the ActiveRel model does. I believe we had a discussion about it a while ago. Chris, do you recall? – Brian Underwood Feb 05 '15 at 09:59
  • just realized we don't even need to order by relationship.. we can just order by the `created_at` for nodes – Clam Feb 11 '15 at 08:23