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.
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