ids = ["4", "1", "3", "2"]@users = User.where(id: ids)# Получаю сейчас
User.where(id: ids) # User1, User2, User3, User4
# Хочу получить
User.where(id: ids) # User4, User1, User3, User2 class Something < ActiveRecrd::Base
scope :for_ids_with_order, ->(ids) {
order = sanitize_sql_array(
["position(id::text in ?)", ids.join(',')]
)
where(:id => ids).order(order)
}
end
# usage:
Something.for_ids_with_order([1, 3, 2])DEPRECATION WARNING: Dangerous query method (method whose arguments are used as raw SQL) called with non-attribute argument(s)
Non-attribute arguments will be disallowed in Rails 6.0. This method should not be called with user-provided values, such as request parameters or model attributes
| id | name |
|---|---|
| 1 | col1 |
| 2 | col2 |
| id | name | params |
|---|---|---|
| 1 | user1 | [{"col_id": 1, "place": 2}, {"col_id": 2, "place": 1}] |
| 2 | user2 | [{"col_id": 1, "place": 1}, {"col_id": 2, "place": 2}] |