There is a model named Track:
class Track < ApplicationRecord
has_many :profile_tracks
has_many :favorite_tracks
has_many :bookmark_tracks
has_many :listened_tracks
end
And there is a model named Profile:
class Profile < ApplicationRecord
has_many :profile_tracks
has_many :favorite_tracks
has_many :bookmark_tracks
has_many :listened_tracks
end
I need to retrieve entries from those tables that share the same track_id and profile_id values. E.g.:
ProfileTrack.find_by(track_id: 1, profile_id: 1)
+
FavoriteTrack.find_by(track_id: 1, profile_id: 1)
+
BookmarkTrack.find_by(track_id: 1, profile_id: 1)
+
ListenedTrack.find_by(track_id: 1, profile_id: 1)
or
track = Track.find_by(id: 1)
track.profile_tracks.find_by(profile_id: 1)
+
track.favorite_tracks.find_by(profile_id: 1)
+
track.bookmark_tracks.find_by(profile_id: 1)
+
track.listened_tracks.find_by(profile_id: 1)
or
profile = Profile.find_by(id: 1)
profile.profile_tracks.find_by(track_id: 1)
+
profile.favorite_tracks.find_by(track_id: 1)
+
profile.bookmark_tracks.find_by(track_id: 1)
+
profile.listened_tracks.find_by(track_id: 1)
and return something like this:
{
profile_track_id: some_id,
favorite_track_id: some_id,
bookmark_track_id: some_id,
listened_track_id: some_id
}
or ActiveRecord model with these ids.
How can I do it in one query?