Order products by association count
问题 Class Product has_many :sales end Class Sale belongs_to :product end How do i get the most sold products.. (Product find all.. order by .. ventas..) ? 回答1: When retrieving the Products, you could do: @products = Product.find(:all, :include => :sales, :order => "sales.value DESC") 'sales.value' can be replaced with whatever value you are trying to order by... EDIT: Disregard the rest of my answer. it won't return the Product objects in descending order by sale value, it'll return the Sales