Product has many product attributes:
class Product < ApplicationRecord
has_many :product_attributes
end
class ProductAttribute < ApplicationRecord
belongs_to :product
end
I can sort it with sort_by
:
@products.includes(:product_attributes).to_a.sort_by do |product|
product.product_attributes.find_by(title: "Volume").value.to_i
end
Is it possible to make the same sort with order
method?
I don’t understand how to order by particular attribute title (like "Volume", etc).
@products = Product.includes(:product_attributes).order( ??? )
Here is similar question:
Rails - Sort by join table data
Maybe I don't see the obvious, but I think it doesn't answer my question. I select item not by attribute name, but by attribute value, like "Volume".
In other words, I find_by
by attribute's value with title "Volume" (look at the code above).
And I don't understand how to make such selection with order
.