Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6353691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:27:20+00:00 2026-05-24T22:27:20+00:00

I have a Rails 3 application I’m working on. I have 3 models: User,

  • 0

I have a Rails 3 application I’m working on. I have 3 models: User, Venue, and Rating.

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

I need to get all venues grouped by neighborhood, with the name of the neighborhood, the average rating for all venues in the neighborhood group, the total number of ratings for all venues in the neighborhood group, the average latitude of all venues in the neighborhood group, and the average longitude of all venues in the neighborhood group.

I’m not quite sure how to get there using ActiveRecord and Postgres, any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:27:20+00:00Added an answer on May 24, 2026 at 10:27 pm

    This is what I’ve done to make it work; however, I feel like their might be a more efficient way to run a postres query and group by neighborhood, but I haven’t had any luck in figuring it out.

    neighborhoods = @venues.select('DISTINCT(venues.neighborhood)').map(&:neighborhood)
    neighborhoods.each do |neighborhood|
        venues_in_neighborhood = @venues.where(:neighborhood => neighborhood).includes(:ratings)
        avg_rating = venues_in_neighborhood.average('ratings.value').to_f        
        number_of_ratings = Rating.includes(:venue).where('venues.neighborhood' => neighborhood).count
        avg_latitude = venues_in_neighborhood.average('latitude').to_f
        avg_longitude = venues_in_neighborhood.average('longitude').to_f
    
        @response << {:name => neighborhood, 
                      :latitude => avg_latitude, 
                      :longitude => avg_longitude, 
                      :rating => avg_rating,
                      :numberOfRatings => number_of_ratings}
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rails application with the following Models: User (id) Version (id, post_id,
I am developing a rails application in which I have two models User and
I have a rails application where each user has a separate database. (taking Joel
I have a rails application which is still showing the cachebusting numeric string at
I have a Rails application where the user can insert text with a WYSIWYG-Editor.
I have a rails application that patches ActiveRecord with a hand-coded validator. The patch
I have a Rails application with several models-views-controllers which have some similar characteristics, for
I have Rails application (Redmine) and I have created next working code which close
I have a Rails application, I have the following DB schema: each User has
I have a rails application in which a user creates a record called a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.