开发者

Optimizing queries with acts_as_taggable_on

开发者 https://www.devze.com 2023-04-05 19:48 出处:网络
Using Rails 3.1 and gem \'acts-as-taggable-on\' version 2.1.1. I have a class: class Meal < ActiveRecord::Base

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 q开发者_Go百科ueries 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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号