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

My solution #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ gem 'activerecord'
gem 'awesome_print'
gem 'sqlite3'
gem 'table_print'
gem 'json', github: 'flori/json', branch: 'v1.8'
12 changes: 11 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
GIT
remote: git://github.com/flori/json.git
revision: 7f4cfd853f2c919d854fb95548a19980feff17e8
branch: v1.8
specs:
json (1.8.6)

GEM
remote: https://rubygems.org/
specs:
Expand All @@ -18,7 +25,6 @@ GEM
awesome_print (1.2.0)
builder (3.2.2)
i18n (0.6.11)
json (1.8.1)
minitest (5.4.2)
sqlite3 (1.3.10)
table_print (1.5.3)
Expand All @@ -32,5 +38,9 @@ PLATFORMS
DEPENDENCIES
activerecord
awesome_print
json!
sqlite3
table_print

BUNDLED WITH
1.15.0
47 changes: 41 additions & 6 deletions associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,38 @@ def generate_migrations
ActiveRecord::Migration.create_table :hotels do |t|
#insert our columns here

t.string :name
t.integer :room_count

t.timestamps null: false
end

ActiveRecord::Migration.create_table :rooms do |t|
#insert our columns here

t.belongs_to :hotel, index: true

t.integer :rate
t.string :location

t.timestamps null: false
end

ActiveRecord::Migration.create_table :bookings do |t|
#insert our columns here

t.belongs_to :user, index: true
t.belongs_to :room, index: true
t.datetime :check_in

t.timestamps null: false
end

ActiveRecord::Migration.create_table :users do |t|
#insert our columns here

t.string :name

t.timestamps null: false
end
end
Expand All @@ -47,25 +61,46 @@ def generate_migrations

class Hotel < ActiveRecord::Base
#insert our associations here


has_many :rooms
has_many :bookings, through: :rooms

def to_s
"#{name} with #{rooms.count} rooms"
end

def booked_guests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out ActiveRecord's has_many :through association. I think it can take care of this.

guests = Array.new
self.bookings.each do |booking|
guests << booking.guest
end
end

end

class Booking < ActiveRecord::Base
#insert our associations here

belongs_to :guest, class_name: "User", foreign_key: "user_id"
belongs_to :room
end

class Room < ActiveRecord::Base
#insert our associations here

has_many :bookings
belongs_to :hotel
end

class User < ActiveRecord::Base
#insert our associations here

has_many :bookings

def booked_rooms
rooms = Array.new
self.bookings.each do |booking|
rooms << booking.room
end
rooms
end
end

#DO NOT CHANGE ANYTHING BELOW THIS LINE.
Expand All @@ -74,11 +109,11 @@ def random_loc; (('a'..'e').to_a.sample) + rand(1..5).to_s; end

hotel = Hotel.create!(name: "Westin", room_count: 5)

5.times do
5.times do
hotel.rooms << Room.create!(
rate: [125,200,175].sample,
location: random_loc
)
)
end

user = User.create!(name: "John Smith")
Expand Down