Optimizing queries with acts_as_taggable_on

微笑、不失礼 提交于 2019-12-11 04:04:27

问题


Using Rails 3.1 and gem 'acts-as-taggable-on' version 2.1.1.

I have a class:

class Meal < ActiveRecord::Base
  acts_as_taggable_on :foods
  ...
end

I have several different scopes on Meal that I use on a dashboard-type page. In the controller, I call, for example:

def index
  @today = Meal.from_today
  @yesterday = Meal.from_yesterday
end

I iterate over @today and @yesterday separately on the dashboard page.

I'd like to optimize the database calls. Right now, I call <%= meal.food_list %> in the view while iterating over each meal in both @today and @yesterday. For each meal, it queries the database to find the foods.

I've been trying to chain the queries in the controller with something like:

@today = Meal.from_today.includes(:foods)

but that doesn't work. Given this situation, how should I optimize the queries? Am I misusing acts-as-taggable-on?

来源:https://stackoverflow.com/questions/7478651/optimizing-queries-with-acts-as-taggable-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!