Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Done #272

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Done #272

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion app/models/city.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@

class City < ActiveRecord::Base
has_many :neighborhoods
has_many :listings, :through => :neighborhoods
has_many :reservations, :through => :listings

end
# Returns all of the available apartments in a city, given the date range
def city_openings(start_date, end_date)
booked_listings = "select distinct(l.id) FROM cities c JOIN neighborhoods n on c.id = n.city_id JOIN listings l on n.id = l.neighborhood_id JOIN reservations r on l.id = r.listing_id WHERE r.checkin >= #{start_date} AND r.checkout <= #{end_date}"
Listing.find_by_sql("select l.id, l.title FROM cities c JOIN neighborhoods n on c.id = n.city_id JOIN listings l on n.id = l.neighborhood_id WHERE l.id NOT IN (#{booked_listings})")
end

# Returns city with highest ratio of reservations to listings
def self.highest_ratio_res_to_listings
query = "select aggregate.id, aggregate.name, aggregate.listing_num, count(r.id) as res_num, count(cast(r.id as decimal)) / cast(listing_num as decimal) as ratio from (select c.id, c.name, count(l.id) as listing_num from cities c
JOIN neighborhoods n on c.id = n.city_id
JOIN listings l on n.id = l.neighborhood_id
group by c.id, c.name) as aggregate
JOIN neighborhoods n on aggregate.id = n.city_id
JOIN listings l on n.id = l.neighborhood_id
JOIN reservations r on l.id = r.listing_id
group by aggregate.id, aggregate.name, aggregate.listing_num
ORDER BY ratio DESC LIMIT 1"
City.find_by_sql(query).first
end

# Returns city with most reservations
def self.most_res
City.find_by_sql("select c.id, c.name, COUNT(c.id) as num_res from cities c JOIN neighborhoods n on c.id = n.city_id JOIN listings l on n.id = l.neighborhood_id JOIN reservations r on l.id = r.listing_id group by c.id, c.name ORDER BY num_res DESC LIMIT 1").first
end

end
42 changes: 0 additions & 42 deletions spec/models/city_spec.rb

This file was deleted.

245 changes: 0 additions & 245 deletions spec/models/listing_spec.rb

This file was deleted.

44 changes: 0 additions & 44 deletions spec/models/neighborhood_spec.rb

This file was deleted.

Loading