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

i have healthy relationships. #51

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
46 changes: 22 additions & 24 deletions associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,34 @@
def setup
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
end

def migrate
ActiveRecord::Migrator.up "db/migrate"
end


def generate_migrations
ActiveRecord::Migration.create_table :hotels do |t|
#insert our columns here

t.integer "room_count"
Copy link
Member

Choose a reason for hiding this comment

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

lgtm however i'd recommend using symbols here instead for column names.

see this post from late but legendary jim weirich:
https://groups.google.com/d/msg/columbusrb/cj-3jepBK1I/w2N5rYkzAqQJ

Copy link
Member

Choose a reason for hiding this comment

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

I hadn't seen that post before. Thanks for the link!

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

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

t.integer "rate"
t.integer "hotel_id"
t.string "location"
t.timestamps null: false
end

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

ActiveRecord::Migration.create_table :users do |t|
t.string "name"
t.timestamps null: false
end

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

ActiveRecord::Migration.create_table :bookings do |t|
t.integer "room_id"
t.integer "user_id"
t.timestamps null: false
end
end


## Do Not Modify These Lines ##
setup()
generate_migrations()
Expand All @@ -46,25 +41,28 @@ def generate_migrations


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

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

alias_attribute :check_in, :created_at
belongs_to :room
belongs_to :guest, foreign_key: "user_id", class_name: "User"
end

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

belongs_to :hotel
has_many :bookings
end

class User < ActiveRecord::Base
#insert our associations here
has_many :booked_rooms, through: :bookings, source: :room
has_many :bookings

end

Expand All @@ -74,11 +72,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