Skip to content

Instantly share code, notes, and snippets.

@lizdenhup
Last active October 22, 2016 02:02
Show Gist options
  • Save lizdenhup/5276a569d234354a222239895940d243 to your computer and use it in GitHub Desktop.
Save lizdenhup/5276a569d234354a222239895940d243 to your computer and use it in GitHub Desktop.
module AirBnbMod
module InstanceMethods
end
module ClassMethods
end
end
class City < ActiveRecord::Base
include AirBnbMod
has_many :neighborhoods
has_many :listings, :through => :neighborhoods
has_many :reservations, :through => :listings
def city_openings(start_date, end_date)
available_reservations = []
listings.each do |listing|
available = listing.reservations.all? do |res|
Date.parse(start_date) >= res.checkout || Date.parse(end_date) <= res.checkin
end
if available
available_reservations << listing
end
end
available_reservations
end
def self.highest_ratio_res_to_listings
City.all.max_by do |city|
res = city.reservations.count
city_listings = city.listings.count
res.to_f/city_listings
end
end
def self.most_res
City.all.max_by do |city|
city.reservations.count
end
end
end
class Neighborhood < ActiveRecord::Base
include AirBnbMod
belongs_to :city
has_many :listings
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment