1
I have 3 tables:
[users] 1 ------------- n [valuations] n -------- 1 [items]
Where evaluation can be positive(evaluation: true) or negative(assessment: false), i need a method that returns all the items that the user liked and did not like... For this I made the following method in the user’s model:
# Retorna o numero de Likes
def likes
itens = Array.new
self.avaliacoes.where( avaliacao: true ).includes(:item).each do |avaliacao|
itens << avaliacao.item
end
return itens
end
The same principle was adopted for the method for the method dislike
The idea is to do this with many users, the problem is that it is taking considerably to return the data. There is some way to improve the performance of that chunk of code? Any structure that I can keep on the server, because this query runs many times more than once per user...
Have you ever thought of using
has many through
? So you could do@usuario.itens
directly, without needing the.each
. Behold– user7261
Checks if the table evaluations has index for user_id and another for item_id.
– GuiGS
@Andrey, I already have
has many through
but I still need to check if the evaluation was positive or negative. It has how to do this?– Vitor Vezani
Would I give something like
self.itens.joins(:avaliacoes).where("avaliacoes.avaliacao = ?", true)
?– user7261