我有以下模式:
- User
- Playlist
- SongLike
- Song
- ...
当我询问其所有歌曲的演唱者时,我收到了一系列的歌曲。 找到用户“喜欢”的这些歌曲中哪些是最有效的方法? 类似情况储存在SongLike模式中:
class SongLike < ActiveRecord::Base
belongs_to :user
belongs_to :song, :counter_cache => "likes_count"
end
......这是歌曲模式:
class Song < ActiveRecord::Base
has_and_belongs_to_many :playlists
has_many :featured_songs
has_many :song_likes
has_many :users, :through => :song_likes
...
end