1
は、ここで私が行うことができるようにしたいどのようなhas_many:through?
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
のようなことができるのが大好きです。