读书人

急 查询的location雷同的字段的数目

发布时间: 2012-12-31 11:57:51 作者: rapoo

急 查询的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


当您的问题得到解答后请及时结贴.
http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
[解决办法]
@bookings = Booking.find_by_sql("select ...")

读书人网 >Ruby Rails

热点推荐