有一個模型名為 Track:
class Track < ApplicationRecord
has_many :profile_tracks
has_many :favorite_tracks
has_many :bookmark_tracks
has_many :listened_tracks
end
還有一個名為 Profile 的模型:
class Profile < ApplicationRecord
has_many :profile_tracks
has_many :favorite_tracks
has_many :bookmark_tracks
has_many :listened_tracks
end
我需要從那些共享相同track_id和profile_id值的表中檢索條目。例如:
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)
或者
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)
或者
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)
并回傳如下內容:
{
profile_track_id: some_id,
favorite_track_id: some_id,
bookmark_track_id: some_id,
listened_track_id: some_id
}
或具有這些 id 的 ActiveRecord 模型。
我怎樣才能在一個查詢中做到這一點?
uj5u.com熱心網友回復:
如果我理解正確,您正在嘗試創建某種音樂播放串列應用程式?您可能希望將您的has_manyin Track更改為belongs_to,但這取決于您的其他模型。無論如何,您應該可以使用joins直接訪問它們。
track = Track.joins(:profile_tracks, :favorite_tracks, :bookmark_tracks,
:listened_tracks).find_by(id: 1)
您可以使用find(1),如果找不到,這將引發錯誤,find_by(id:1)將回傳 nil。然后在你的哈希
{
profile_track_id: track.profile_track_id,
favorite_track_id: track.favorite_track_id,
bookmark_track_id: track.bookmark_track_id,
listened_track_id: track.listened_track_id
}
更新
為了與問題保持一致,因為上面的答案是錯誤的,但仍然可能有用。
track = Track.joins(:profile_tracks, :favorite_tracks, :bookmark_tracks,
:listened_tracks, :profiles).where(profiles: {id: 1})
這將產生以下查詢
SELECT "tracks".* FROM "tracks"
INNER JOIN "profile_tracks" ON "profile_tracks"."track_id" = "tracks"."id"
...
INNER JOIN "profiles" ON "profiles"."track_id" = "tracks"."id"
WHERE "profiles"."id" = $1
更新 2
感謝您指出軌道和輪廓之間不存在關系,重新排列結構怎么樣?
class Profile < ApplicationRecord
has_many :profile_tracks
has_many :tracks, through: :profile_tracks
end
class ProfileTrack < ApplicationRecord
belongs_to :profile
has_many :tracks
end
class Track < ApplicationRecord
belongs_to :profile_track
end
這將允許您同時訪問track_ids和profile_track_ids從 Profile
uj5u.com熱心網友回復:
使用原始 SQL 和UNION:
track = Track.find_by(id: 1)
a =
track
.profile_tracks
.where(profile_id: 1)
.select("id as value, 'profile_track_id' as key")
.to_sql
b =
track
.favorite_tracks
.where(profile_id: 1)
.select("id as value, 'favorite_track_id' as key")
.to_sql
c =
track
.bookmark_tracks
.where(profile_id: 1)
.select("id as value, 'bookmark_track_id' as key")
.to_sql
d =
track
.listened_tracks
.where(profile_id: 1)
.select("id as value, 'listened_track_id' as key")
.to_sql
ActiveRecord::Base
.connection
.execute("#{a} UNION #{b} UNION #{c} UNION #{d}")
.to_a
.map {|a| { a['key'] => a['value']}}
.inject(:merge)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/408212.html
標籤:
上一篇:為所有保存的物件設定參考欄位
