With a hard-coded array
array = [30,29,31,13,10,12,6,7,8,9,11]
trying to execute a query
@pick = Item.where('id IN (?)', array).to_a
How can the order of Items chosen keep the order of initial array?
With a hard-coded array
array = [30,29,31,13,10,12,6,7,8,9,11]
trying to execute a query
@pick = Item.where('id IN (?)', array).to_a
How can the order of Items chosen keep the order of initial array?
Assuming you're fetching all the items in a single request (i.e. no pagination) then you could sort the items after fetching using the indices from the initial array e.g.
@pick = Item.where('id IN (?)', array).sort_by do |item|
array.index(item.id)
end
It is a good approach to tell the database the preferred order and load all records sorted in that order directly from the database instead of sorting the records in Ruby.
Add the following extension to your application:
# e.g. in config/initializers/find_by_ordered_ids.rb
module FindByOrderedIdsActiveRecordExtension
extend ActiveSupport::Concern
module ClassMethods
def find_ordered(ids)
order_clause = "CASE #{self.table_name}.id "
ids.each_with_index do |id, index|
order_clause << "WHEN #{id} THEN #{index} "
end
order_clause << "ELSE #{ids.length} END"
where(id: ids).order(order_clause)
end
end
end
ActiveRecord::Base.include(FindByOrderedIdsActiveRecordExtension)
Than you can write queries like this:
Item.find_ordered([2, 1, 3]) # => [2, 1, 3]
include FindByOrderedIdsActiveRecordExtension in ApplicationRecord in Rails5 though.ERROR: syntax error at or near "{" when the module fires up. I do not see where the proposed code can be off!# in front of the {self.table_name} in the first line of the find_ordered method. I updated my answer.