0

I just want to make sure that if I used a where query like so:

product1, product2 = Product.where(id: [1, 2]) it will always return the data in that order specified in the array, so the assignment will always be correct.

Is that the behaviour or it might not return the data in that specific order at some time?

1 Answer 1

2

Records will always be fetched from database in ascending order by id column (unless you specify another order).

So yea, this is the behavior and you'll always get the assignment right here.

Sign up to request clarification or add additional context in comments.

4 Comments

That answers my question, but I have another one please, what if I don't know the IDs which I am passing, then how to make sure that I will get the correct assignment? Product.where(id: [id1, id2])
can you provide the example, so I understand exactly what you mean?
id1 = 2, id2 = 1 p1, p2 = Product.where(id: [id1, id2]) Sorting the IDs won't help, as the assignment ordering will not be sorted relatively.
@IslamWazery I think you could find some hints in this article

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.