On Sunday, December 4, 2016 at 9:00:04 PM UTC, Brian Christian wrote: > > > Okay, so my issue here is: > > If the cache key is just based on the Posts, then the controller code will > pull the Authors association for no reason (we don’t actually need it, > because the author names are cached until the Post models are touched). > This makes me think I should remove the .includes() call from my > controller, since it will cause an unnecessary db query 99% of the time. > > On the other hand, if I remove the .includes() call from my controller, > then whenever the cache *does* expire, I have an *n*-queries situation, > which also seems bad. > > Is there any way to have the best of both worlds here? > > The :preload variant of includes can be run after the query has run -
ActiveRecord::Associations::Preloader.new.preload(@recent, :authors) Will load that association on @recent (you can preload multiple or nested associations in the same way. You could do this once you've gone down the cache miss case. > A related question: > > I’ve also found it helpful to use lower-level caching in my Post model to > cache the “recent” query like so: > > post.rb > self.recent do > Rails.cache.fetch('recent_posts’, expires_in: 1.day) do > self.order(created_at: :desc).to_a > end > end > > Here I have a similar problem. If I don’t include the `.to_a` at the end, > then I’m only caching the *scope*, which will still evaluate and > seemingly (at least in development) trigger a db query on every pageview. > If I *do* include the `.to_a` then I forfeit the ability to use > `.includes()` in my individual controller actions or views. > > Again, is there a way to have the best of both worlds here? > > You can use the same trick here. Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscr...@googlegroups.com. To post to this group, send email to rubyonrails-talk@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/92233b62-e866-43ae-94ac-3bb029c0ef92%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.