Answer the question
In order to leave comments, you need to log in
How to convert array to relation?
I know that these are the wrong actions initially, but still, in my case I see no other way out.
There are goods - model Items (table items).
The price for them is built dynamically - the PriceItems model (there is no table).
I need to sort goods by price, I do
@products.map do |product|
price = @prices["#{product.make_name}-#{product.oem}"] #здесь получаю цену конкретного товара
if price.present?
[price.cost, product]
end
end
j = s.sort_by {|h| h[0]} # сортирую по цене
w = j.each {|el| el.delete_at(0)} # удаляю цену и получаю [[#<Product id: 3], [#<Product id: 4], [#<Product id: 2], [#<Product id: 1] ]
@sort_products = w.flatten #Получаю массив с объектами [#<Product id: 3, #<Product id: 4, #<Product id: 2, #<Product id: 1]
ActiveRecord::Relation
? Items.where(id: @sort_products.map(&:id))
Answer the question
In order to leave comments, you need to log in
You cannot convert array to ActiveRecord::Relation. Since Relation is a build for a SQL query.
Building the price dynamically every time is a bad idea, you need to store such things in the database.
Sorting is spoiled to you by the basis, means and the decision needs to be looked for at the level of a database. For postgres, choose any here - https://stackoverflow.com/questions/866465/order-b... The
query will have to be built dynamically, but this is not particularly difficult.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question