【发布时间】:2015-03-26 13:36:11
【问题描述】:
在 Rails 中,当有 ActiveRecord 关联时,说:
class Song < ActiveRecord::Base
has_many :artists_songs, :class_name => "ArtistSong"
has_many :artists, :class_name => "Artist", :through => :artists_songs
end
class Artist< ActiveRecord::Base
has_many :artists_songs, :class_name => "ArtistSong"
has_many :songs, :class_name => "Song", :through => :artists_songs
end
class ArtistSong < ActiveRecord::Base
belongs_to :song, :class_name => "Song"
belongs_to :artist, :class_name => "Artist"
end
并说我访问了这样的歌曲
my_song = Song.joins(:artists).first
-
为什么我第一次访问艺人协会时会这样
my_song.artists
执行了一个新的 SQL 查询,尽管“joins”命令已经加载了艺术家列表?
-
在首次加载艺术家关联后(以便构建缓存),有没有办法过滤艺术家无需再次加载? 因为
my_song.artists.where(Id: 55)
每次执行时都会执行一个新查询,即使有可用的缓存。
谢谢。
【问题讨论】:
标签: ruby-on-rails caching activerecord