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

onboarding activity #32

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
49 changes: 31 additions & 18 deletions associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,32 @@ def migrate


def generate_migrations
ActiveRecord::Migration.create_table :hotels do |t|
#insert our columns here
#Users & Hotel belong to no one => they can claim belongs_to rels

ActiveRecord::Migration.create_table :hotels do |t|
t.string :name, null: false
t.integer :room_count, default: 0
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, null: false
t.string :location, null: false
t.timestamps null: false
end

#owns belongs to room, user; bridge of user to room
ActiveRecord::Migration.create_table :bookings do |t|
#insert our columns here

t.belongs_to :room, index: true
t.belongs_to :user, index: true
t.belongs_to :hotel, 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, null: false
t.timestamps null: false
end
end
Expand All @@ -45,27 +50,34 @@ def generate_migrations
##### ^^ Do Not Modify ^^ ####


#user owns rooms and bookings, and users through bookings
class Hotel < ActiveRecord::Base
#insert our associations here

has_many :rooms
has_many :bookings, through: :rooms
has_many :booked_guests, through: :bookings, source: :guest
def to_s
"#{name} with #{rooms.count} rooms"
end
end

#establishes connection between room and user
class Booking < ActiveRecord::Base
#insert our associations here

belongs_to :guest, class_name: 'User', foreign_key: 'user_id'
belongs_to :room
belongs_to :hotel
end

#room is owned by hotel
class Room < ActiveRecord::Base
#insert our associations here

belongs_to :hotel
has_many :bookings
has_many :users, through: :bookings
end

#user has many bookings, has rooms through those bookings
class User < ActiveRecord::Base
#insert our associations here

has_many :bookings
has_many :booked_rooms, through: :bookings, source: :room
end

#DO NOT CHANGE ANYTHING BELOW THIS LINE.
Expand All @@ -74,15 +86,16 @@ 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")
room = hotel.rooms.first
#changed attribute 'guest' to 'user'
b = Booking.create!(guest: user, room: room, check_in: Time.now)

line_sep("#{user.name} bookings")
Expand Down