{"action":"created","issue":{"url":"https://api.github.com/repos/neo4jrb/neo4j/issues/720","labels_url":"https://api.github.com/repos/neo4jrb/neo4j/issues/720/labels{/name}","comments_url":"https://api.github.com/repos/neo4jrb/neo4j/issues/720/comments","events_url":"https://api.github.com/repos/neo4jrb/neo4j/issues/720/events","html_url":"https://github.com/neo4jrb/neo4j/issues/720","id":61929446,"number":720,"title":"QueryProxy#first and #last return same object in ordered multi-object list","user":{"login":"posgarou","id":8039360,"avatar_url":"https://avatars.githubusercontent.com/u/8039360?v=3","gravatar_id":"","url":"https://api.github.com/users/posgarou","html_url":"https://github.com/posgarou","followers_url":"https://api.github.com/users/posgarou/followers","following_url":"https://api.github.com/users/posgarou/following{/other_user}","gists_url":"https://api.github.com/users/posgarou/gists{/gist_id}","starred_url":"https://api.github.com/users/posgarou/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/posgarou/subscriptions","organizations_url":"https://api.github.com/users/posgarou/orgs","repos_url":"https://api.github.com/users/posgarou/repos","events_url":"https://api.github.com/users/posgarou/events{/privacy}","received_events_url":"https://api.github.com/users/posgarou/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-03-16T00:46:03Z","updated_at":"2015-03-16T18:36:27Z","closed_at":null,"body":"I ran into some curious behavior today.\r\n\r\nI have the following class and method:\r\n\r\n```ruby\r\nclass Recipe\r\n    has_many :out, :steps, type: :has_step, model_class: Graph::RecipeStep\r\n\r\n    def steps_in_order\r\n      steps.order(number: :asc)\r\n    end\r\nend\r\n```\r\n\r\nWhen I run `foo.steps_in_order.to_a`, everything looks normal: it has been sorted successfully by the number property.\r\n\r\n```\r\n[1] pry(#<RSpec::ExampleGroups::GraphRecipe::StepsInOrder>)> ordered_ingredients.to_a\r\n[\r\n    [0] #<Graph::RecipeStep number: 1, optional: false, text: \"You put your left foot in, you take your left foot out\">,\r\n    [1] #<Graph::RecipeStep number: 2, optional: false, text: \"You put your left foot in, you take your left foot out\">,\r\n    [2] #<Graph::RecipeStep number: 3, optional: false, text: \"You put your left foot in, you take your left foot out\">,\r\n    [3] #<Graph::RecipeStep number: 4, optional: false, text: \"You put your left foot in, you take your left foot out\">,\r\n    [4] #<Graph::RecipeStep number: 5, optional: false, text: \"You put your left foot in, you take your left foot out\">,\r\n    [5] #<Graph::RecipeStep number: 6, optional: false, text: \"You put your left foot in, you take your left foot out\">\r\n]\r\n```\r\n\r\nBut if I call `#first` and `#last` directly on the QueryProxy object without calling `#to_a` first, then both methods return the first element:\r\n\r\n```ruby\r\n[2] pry(#<RSpec::ExampleGroups::GraphRecipe::StepsInOrder>)> ordered_ingredients.first\r\n#<Graph::RecipeStep number: 1, optional: false, text: \"You put your left foot in, you take your left foot out\">\r\n[3] pry(#<RSpec::ExampleGroups::GraphRecipe::StepsInOrder>)> ordered_ingredients.last\r\n#<Graph::RecipeStep number: 1, optional: false, text: \"You put your left foot in, you take your left foot out\">\r\n```\r\n\r\nIf I change the method above to `order(number: :desc)`, then `#first` and `#last` still return the same element, but this time it is the element that comes first in descending order (the step with number 6):\r\n\r\n```ruby\r\n[2] pry(#<RSpec::ExampleGroups::GraphRecipe::StepsInOrder>)> ordered_ingredients.first\r\n#<Graph::RecipeStep number: 6, optional: false, text: \"You put your left foot in, you take your left foot out\">\r\n[3] pry(#<RSpec::ExampleGroups::GraphRecipe::StepsInOrder>)> ordered_ingredients.last\r\n#<Graph::RecipeStep number: 6, optional: false, text: \"You put your left foot in, you take your left foot out\">\r\n```\r\n\r\nIf I call `#first` and `#last` without ordering the query (`[recipe.steps.first, recipe.steps.last]`), everything works as expected.\r\n\r\nI'm using Neo4j 4.1.4 and neo4j-core 4.0.4"},"comment":{"url":"https://api.github.com/repos/neo4jrb/neo4j/issues/comments/81858970","html_url":"https://github.com/neo4jrb/neo4j/issues/720#issuecomment-81858970","issue_url":"https://api.github.com/repos/neo4jrb/neo4j/issues/720","id":81858970,"user":{"login":"subvertallchris","id":4097271,"avatar_url":"https://avatars.githubusercontent.com/u/4097271?v=3","gravatar_id":"","url":"https://api.github.com/users/subvertallchris","html_url":"https://github.com/subvertallchris","followers_url":"https://api.github.com/users/subvertallchris/followers","following_url":"https://api.github.com/users/subvertallchris/following{/other_user}","gists_url":"https://api.github.com/users/subvertallchris/gists{/gist_id}","starred_url":"https://api.github.com/users/subvertallchris/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/subvertallchris/subscriptions","organizations_url":"https://api.github.com/users/subvertallchris/orgs","repos_url":"https://api.github.com/users/subvertallchris/repos","events_url":"https://api.github.com/users/subvertallchris/events{/privacy}","received_events_url":"https://api.github.com/users/subvertallchris/received_events","type":"User","site_admin":false},"created_at":"2015-03-16T18:36:27Z","updated_at":"2015-03-16T18:36:27Z","body":"Really cool find. I'm guessing that this has something to do with using the `ORDER` function twice within one Cypher statement, which is how we've been handling `first` and `last`. I think the solution is going to be using `COLLECT` with `HEAD` or `LAST` but it might result in some unexpected results since what we think of as \"first\" might be different from Neo4j's thoughts."}}