Skip to content

Instantly share code, notes, and snippets.

@mateuszbialowas
Created May 25, 2022 17:36
Show Gist options
  • Save mateuszbialowas/f48fd7ca4ca51d963cfdfb745e701191 to your computer and use it in GitHub Desktop.
Save mateuszbialowas/f48fd7ca4ca51d963cfdfb745e701191 to your computer and use it in GitHub Desktop.
ar-mapping-custom-pk-fk
# frozen_string_literal: true
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'pry'
gem "sqlite3"
gem 'activerecord'
end
require 'active_record'
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :bookings, primary_key: :booking_id, force: true do |t|
t.datetime :start_date, null: false
t.datetime :end_date, null: false
t.belongs_to :room, null: false
t.belongs_to :guest, null: false
end
create_table :rooms, primary_key: :room_id, force: true do |t|
t.integer :room_number, null: false, default: 1, unique: true
t.string :room_type, null: false, default: 'standard_room'
end
create_table :guests, primary_key: :guest_id, force: true do |t|
t.string :guest_name, null: false
t.string :guest_passport_no, null: false, unique: true
t.string :guest_phone, null: false , unique: true
end
add_foreign_key :bookings, :rooms, column: :room_id, primary_key: :room_id
add_foreign_key :bookings, :guests, column: :guest_id, primary_key: :guest_id
end
class Booking < ActiveRecord::Base
self.primary_key = :booking_id
belongs_to :room
belongs_to :guest
end
class Guest < ActiveRecord::Base
self.primary_key = :guest_id
has_many :bookings
end
class Room < ActiveRecord::Base
self.primary_key = :room_id
has_many :bookings
end
Booking.create!(
start_date: Date.new,
end_date: Date.new,
room: Room.create,
guest: Guest.create(guest_name: :sth, guest_passport_no: :sth, guest_phone: :sth)
)
require 'pry'
binding.pry
@lesterzone
Copy link

lesterzone commented Aug 12, 2022

I wanted to try this on docker:

Dockerfile

FROM ruby:3.0

WORKDIR /usr/src/app

RUN gem install 'pry' && gem install 'sqlite3' && gem install 'activerecord'

COPY . .

# CMD ["./scratch.rb"]

Then build and run:

docker build -t active_record_playground .
docker run -it active_record_playground ruby scratch.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment