Reputation: 37
I have this associations:
class Ship < ApplicationRecord
has_many :captain_profiles
has_many :captains, through: :captain_profiles
end
class CaptainProfile < ApplicationRecord
belongs_to :captain
belongs_to :ship
end
class Captain < ApplicationRecord
has_one :captain_profile
has_many :schedules
end
class Schedule < ApplicationRecord
belongs_to :captain
end
And I need list of all ships that are ready to be taken to the sea. In other words I have to find all ships that has at least one Captain with at least one of his schedules.
I thought about merging two inner joins as I need Ships which has Captains which has Schedules.
I tried Captain.includes(:schedules).where("schedule.id IS NOT NULL")
and so with Ships but it does not work. Could someone explain me what am I doing wrong and how shall I do this?
Upvotes: 0
Views: 30
Reputation: 5313
Simply use joins
which generates INNER JOIN
.
Ship.joins(captains: :schedules)
Upvotes: 1