Taking advantage of the answer from @bfavaretto, if you have in Avaliacao
a belongs_to :item
, you should do the following for #:
ruby-on-rails
Item.joins(:avaliacaos).group("avaliacaos.item_id").order("sum(avaliacaos.avaliacao) desc")
Otherwise, it will be necessary to iterate over the elements of the Item
items = Hash.new
Item.all.each do |item|
items[item.id] = Avaliacao.where('item_id = ? AND avaliacao = ?', item.id, 1).count
end
maiores_avaliacoes = items.sort_by { |k,v| v}.reverse
Where maiores_avaliacoes
will be a vector of pairs in which the first element is the key and the second the number of positive evaluations, that is, maiores_avaliacoes[0]
will return a vector with two positions where / em> will be the item_id
of the item with the most positive ratings and the position 1 will be the number of positive ratings that that item has.
This should return only the items with ratings and that are positive.
How to count the number of positive and negative evaluations
Due to my inability with SQL
queries, I will not know the query that should be made to return the number of positive or negative evaluations (though I suppose that count
should be used instead of sum
) . However, you can achieve what you want by slightly modifying the second example, as follows:
avaliacoes = Hash.new
Item.all.each do |item|
avaliacoes[item.id] = [Avaliacao.where('item_id = ? AND avaliacao = ?', item.id, 0).count, Avaliacao.where('item_id = ? AND avaliacao = ?', item.id, 1).count]
end
Item.all.each do |item|
puts item.nome + " - <" + avaliacoes[item.id][1] + " positivas> <" + avaliacoes[item.id][1] + " negativas>\n"
end
This prints the data in the form you want, remembering that index 1 counts the number of positive ratings, while index 0 counts the negative >, saving them in Hash
using item.id
.