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

working now #27

Open
wants to merge 1 commit 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
38 changes: 25 additions & 13 deletions associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,34 @@ def migrate
def generate_migrations
ActiveRecord::Migration.create_table :hotels do |t|
#insert our columns here

t.string :name, null: false
t.integer :room_count, null: false
t.timestamps null: false
end

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

t.integer :rate, null: false
t.string :location, null: false
t.references :hotel, index: true, foreign_key: true
t.timestamps null: false
end

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

t.string :name, null: false
t.timestamps null: false
end

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

t.datetime :check_in, null: false
t.integer :guest, index: true
t.integer :room, index: true
t.references :hotel, index: true, foreign_key: true
t.timestamps null: false
end

end


Expand All @@ -46,26 +53,31 @@ def generate_migrations


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

#insert our associations
has_many :rooms
has_many :bookings
has_many :booked_guests, class_name: 'Booking', foreign_key: 'guest'
def to_s
"#{name} with #{rooms.count} rooms"
end
end

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

#insert our associations
belongs_to :user
belongs_to :hotel
end

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

belongs_to :hotel
belongs_to :user
end

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

has_many :bookings, foreign_key: 'guest'
has_many :booked_rooms, class_name: 'Booking', foreign_key: 'room'
end

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

user = User.create!(name: "John Smith")
room = hotel.rooms.first
b = Booking.create!(guest: user, room: room, check_in: Time.now)
b = Booking.create!(guest: user.id, room: room.id, check_in: Time.now, hotel: hotel)

Choose a reason for hiding this comment

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

You shouldn't need to store a hotel in a booking - you're already storing a room which by definition has a hotel attached.


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