2016-06-10 2 views
0

menu_items (요리와 레스토랑으로 구성된 다른 조인 테이블)와 사용자를 결합하는 조인 테이블 (즐겨 찾기)이 있습니다.Ruby : 테이블 항목의 합계/계산 방법은 무엇입니까?

가장 인기있는 요리 (테이블에서 가장 자주 나오는 요리)와 각 요리의 최고 식당을 나열하는 표를 만들고 싶습니다. 나는 그곳에있는 대부분의 방법이지만 카운트를하는 방법을 모릅니다. 어떤 팁?

<div class="row"> 
<div class="col-md-12"> 
<table class="table table-striped table-hover"> 
    <tr> 
    <!-- <th>Dish comment</th> --> 
    <th>Dish</th> 
    <th>Restaurant</th> 
    <th>User</th> 
    <th>Actions</th> 
    </tr> 

    <% @favorites.each do |favorite| %> 
    <tr> 
    <!-- <td><%= favorite.dish_comment %></td> --> 
    <td><%= Dish.find(favorite.dishing_id).dish_name %></td> 
    <% d = Dishing.find(favorite.dishing_id) %> 
    <td><%= Restaurant.find(d.restaurant_id).name %></td> 
    <td><%= User.find(favorite.user_id).email %></td> 

답변

1

보십시오 그룹, 후 가입 개수 : Most Popular Dishes

여기 내 코드입니다.

User.joins(menu_items) 
      .select(count(menu_items.id) as dish_count, menu_items.dish_name, users.name) 
      .where(<conditions>) 
      .group(menu_items.dish_name) 
+0

고마워요! 이 코드는 어디에서 볼 수 있습니까? 여기 아래? <% @ favorites.each do | 즐겨 찾기 | %> –

+0

컨트롤러에서 시도하십시오. –

관련 문제