내 현재 모델은 다음과 같습니다.
class Artist < ApplicationRecord
has_many :albums
has_many :follows
has_many :users, -> { uniq }, through: :follows
end
class Album < ApplicationRecord
belongs_to :artist
end
class Follow < ApplicationRecord
belongs_to :artist
belongs_to :user
end
class User < ApplicationRecord
has_many :follows
has_many :artists, -> { uniq }, through: :follows
end
내가 할 수 있기를 원하는 Albums
것은 사용자를 위해 모든 것을 얻는 것 입니다.
아티스트를 쉽게 구할 수 @user.artists
있지만 ( ), 그 아티스트의 앨범이 전부 다들 문제가 있어요.
Artists
모델 을 Users
통해 연결됩니다 Follows
.
@users.albums
또는 같은 것을 할 수 있기를 원합니다 @users.artists.albums
.
당신이 user has_many :artists
와artist has_many :albums
모델 에서 has_many
연결을 생성하십시오.User
album :through artists
class User < ApplicationRecord
has_many :follows
has_many :artists, -> { uniq }, through: :follows
has_many :album, through: :artists
end
이제 사용할 수 있습니다 @user.albums
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다