急 查询的location相同的字段的数目
Tables:
booking: show_id (belongs_to: show)
show: name, venue_id (has_many :bookings belongs_to :venue)
venue: location, capacity ( has_many : shows)
我的这个页面的代码:
<h1>Listing venue rank</h1>
<% @bookings.each do |booking| %>
<tr>
<td class="large box">
<%=h booking.show.venue.location %>
<br />
</tr></table>
现在我的页面显示如下:
Listing venue rank
Location: www
Location: www
Location: weee
Location: weee
Location: www
我想让我的界面显示如下
Listing venue rank
Location: www 3
Location: weee 1
即我想查询的location相同的字段的数目。我的controller代码如下。
class ShowsController < ApplicationController
def index
@shows = Show.find(:all, :order => "date")
@venue = Venue.find(:all)
class BookingsController < ApplicationController
def index
@bookings = Booking.find(:all, :order => "show_id, date")
@venue = Venue.find(:all)
@shows = Show.find(:all)
class VenuesController < ApplicationController
def index
@venues = Venue.all
[解决办法]
select location,count(*)
from venue,show
where venue.venue_id=show.venue_id
group by location