I've got a rails app with the following models:
class Partner < ActiveRecord::Base
has_many :timesheets
has_many :timeslots, through: :timesheets
# ...
end
class Timeslot < ActiveRecord::Base
has_many :timesheets
has_many :partners, through: :timesheets
# ...
end
class Timesheet < ActiveRecord::Base
belongs_to :partner
belongs_to :timeslot
# ...
end
Notice how the Timesheet serves as a join table of sorts for storing Partner-Timeslot pairs. I want to get all such Partner-Timeslot pairs where the timeslots fulfill some condition. I don't actually care at all about the Timesheet object for this query. I've worked out that the appropriate SQL is something along the lines of:
SELECT partners.*, timeslots.*
FROM timesheets
JOIN partners
ON timesheets.partner_id = partners.id
JOIN timeslots
ON timesheets.timeslot_id = timeslots.id
WHERE (...some condition logic that I can figure out on my own...)
Notice how although I'm querying the timesheets table I actually only want Partner and Timeslot objects.
Can you do this with Rails?
Is there a better way to do it?
Miscellaneous info:
I'm using MySQL as the database