我有一个Rails 3应用程序正在工作。我有3个模型:用户,场地和评分。Postgres,Rails和GROUP
User.rb:
# attributes:
# name, email => String
class User < ActiveRecord::Base
has_many :ratings
has_many :venues, :through => :ratings, :uniq => true
...
end
Venue.rb:
# attributes:
# name, address, city, state, zip, neighborhood => String
# latitude, longitude => Float
class Venue < ActiveRecord::Base
has_many :ratings
has_many :users
...
def average_rating
sum = 0
self.ratings.each do |rating|
sum += rating.value
end
sum.to_f/self.number_of_ratings.to_f
end
def number_of_ratings
self.ratings.count
end
end
Rating.rb:
# attributes:
# venue_id, user_id => Integer
# value => String
class Rating < ActiveRecord::Base
belongs_to :user
belongs_to :venue
...
end
我需要通过居委会分组的所有场馆,名称邻里组中所有场馆的平均评分,n中所有场馆的评分总数eighborhood组,邻域组中所有场馆的平均纬度以及邻域组中所有场馆的平均经度。
我不太清楚如何使用ActiveRecord和Postgres来完成任何建议?