Help with a Ruby on Rails query

Asked

Viewed 57 times

-1

Good afternoon, I just started working with Rails and I’m having trouble with a query. I’m building an Api where I have the Game model and the User model. A user has several games, and one game has two users (there being an intermediate Gameusers table). When using the Rails console, I can easily see the users of a game making: inserir a descrição da imagem aqui

83 and 84 are the ids of users belonging to that game. But if I do inserir a descrição da imagem aqui

I can’t do a query to return the games to which two users belong. Can anyone help me with this? Thanks

Updating

After a while I found out it would be Game.joins(:users).where(users: { id: [83 && 84] })

1 answer

0

  • Thank you for your reply helped me, but in that querry are the user games 83 OR 84. The answer would be Game.joins(:users).where(users: { id: [83 && 84] }).

  • Your query is not doing what you think it is doing, it executes this query you commented on and the one I mentioned in the reply and see the difference in the generated SQL query. In your case, you are only using ID 83 in the query, because [83 && 84] results in [83]. Anyway, I don’t quite understand your intention, but I’m glad the answer helped you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.