0

I have this array returned by a activerecord query:

SELECT id, value FROM table ORDER BY value DESC
[1054, 86], [1062, 86], [1059, 84]

And I've created a simple array to compare with the first array:

array.sort_by! { |object| object.access_count }.reverse }

But when I compare the two arrays the result is different:

activerecord => [1054, 86], [1062, 86], [1059, 84]
array.sort_by! => [[1062, 86], [1054, 86], [1059, 84]

So in some cases the compare doesn't work because activerecord sorts one way and sort_by! sorts another.

How can I solve that ? Thanks.

3
  • What you are trying to achieve by comparing both? can you explain a bit more? @gui12344455 Commented Oct 13, 2017 at 12:01
  • yes, I'm using rspec to test my code and one test is to verify if the data returned is sorted correctly. Commented Oct 13, 2017 at 12:15
  • So you are trying test order method of Active record. Actually in RSpec we are responsible to test the code written by us. You no need to test order here. In case you are necessary to write test case for order, In the spec you can make a query like records = Model.only(:id, :name).order(:desc) and check like this expected(response_records).to eq(records) Commented Oct 13, 2017 at 12:37

1 Answer 1

1

Can you try order Activerecord with second arg.

.order(value: :desc, id: :desc)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.