这应该非常简单,但是我在寻找答案时遇到了很多麻烦。我只想要一组通过外键关联到另一组记录的记录。例如,如果我有一个通过Song.artist_id拥有Song的艺术家歌曲,并且我有一组通过Artist.where(mustache: true)
的Artists,则我希望查询返回所有属于所有Artists的Songs。所以像:
@songs = Artist.where(mustache: true).songs
但是确实有效。
Song.where(artist_id: Artist.where(mustache: true).pluck(:id))
或
song.joins(:artist).where(artists: {mustache: true})