mardi 31 octobre 2017

Query - Ruby on Rails

Please, how do I make this query in Rails?

SELECT *, count(v.id) numero_votos FROM ideas i left join votes v on v.idea_id = i.id group by i.id order by numero_votos desc, i.created_at

I tried this...

@listOfIdeas = Idea.find(:all, joins: "LEFT JOIN 'votes' on votes.idea_id = ideas.id",
                                    select: "ideas.*, count(votes.id) numero_de_votos",
                                    group: "ideas.id",
                                    order: "numero_de_votos DESC, ideas.created_at DESC")

But, didn't work, it's showing me this message:

Couldn't find all Ideas with 'id': (all, {:joins=>"LEFT JOIN 'votes' on votes.idea_id = ideas.id", :select=>"ideas.*, count(votes.id) numero_de_votos", :group=>"ideas.id", :order=>"numero_de_votos DESC, ideas.created_at DESC"}) (found 0 results, but was looking for 2)

Rails version: 5.1.4

Thanks!

Aucun commentaire:

Enregistrer un commentaire