Take the 2-minute tour ×

these are my Models

User -> has many Pins
Pins -> has many votes
vote -> has a user_id and pin_id

How do I get all the Pins that are voted by the user in the most effective way? Basically, I want to emulate this query in Ruby.

SELECT * FROM PINS a JOIN VOTES b ON a.id = b.pin_id WHERE b.user_id = current_user.id

How can I do it? I can use, db.execute, but can't I do anything without using SQL?

Thanks for any help.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Seeing that a vote belongs to a user, a user should have_many votes, and you can just get the pins via a has many through association

class User < ActiveRecord::Base
   has_many :votes
   has_many voted_pins, through: :votes, source: :pin 
share|improve this answer
add comment

Your Answer


