I have three models:
- User
- File
- Download
User has_many Files, File belongs_to User
File has_many Downloads, Download belongs_to User
Is there any way to retrieve all a User‘s Downloads, without iterating through all Files, then retrieving the Downloads associated with each file? (this would generate lots of queries)
EDIT
I’ve found a way of loading a User, it’s Files and Downloads in just 3 queries
user = User.includes(:uploads => [:downloads]).find(1)
However, what if I already have a User object?
And what if I just want to load the users downloads without loading their uploads? (using a join)
EDIT
Download.joins(:upload => :user).where(:users => { :id => user.id })
Is there a way of doing this having the reference the users table primary key directly?
have you tried