I am using Ruby on Rails 3.2.2 and I would like to order only one element present in a Array resulting from executing a ActiveRecord::Relation. That is, I have an ActiveRecord::Relation that returns the following Array:
[
#<Article id: 1, title: "Sample title 1", content: "Sample content 1">,
#<Article id: 2, title: "Sample title 2", content: "Sample content 2">,
#<Article id: 3, title: "Sample title 3", content: "Sample content 3">,
#<Article id: 4, title: "Sample title 4", content: "Sample content 4">,
#<Article id: 5, title: "Sample title 5", content: "Sample content 5">,
...
]
I would like to “put” / “order” the Article object with id 4 and title “Sample title 4” to the first position in the Array so to make it possible to output that related object data as the first element in my view files.
How it is possible to make that?
Simply use the
orderto this, it will generate sql from your order, which evaluates to a true or fals condition, and this brings your desired elements first:Quite hardcoded, but it works.