Skip to content

Instantly share code, notes, and snippets.

@raghuvarmabh
Created July 10, 2018 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raghuvarmabh/1d3784225c3be5113be8578710c6ec75 to your computer and use it in GitHub Desktop.
Save raghuvarmabh/1d3784225c3be5113be8578710c6ec75 to your computer and use it in GitHub Desktop.
Bare minimum rails project to experiment on models
# Instead of loading all of Rails, load the
# particular Rails dependencies we need
require 'sqlite3'
require 'active_record'
require 'pry'
# Set up a database that resides in RAM
ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: ':memory:'
)
# Set up database tables and columns
ActiveRecord::Schema.define do
create_table "bookings", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.references :order, polymorphic: true, index: true, null: false
end
create_table "pickup_orders", force: :cascade do |t|
t.string "notes"
t.string "fax"
t.string "pickup_time"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "delivery_orders", force: :cascade do |t|
t.string "notes"
t.string "home_address"
t.string "delivery_time"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
# Set up model classes
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
class Booking < ApplicationRecord
belongs_to :order, polymorphic: true
end
class PickupOrder < ApplicationRecord
has_many :bookings, as: :order, dependent: :destroy, inverse_of: :order
end
class DeliveryOrder < ApplicationRecord
has_many :bookings, as: :order, dependent: :destroy, inverse_of: :order
end
# Try everything out!
booking = Booking.new
booking.name = "Yummy food!"
booking.order = PickupOrder.new(notes: 'need it really tasty', pickup_time: 'in 2 hours')
booking.save
booking = Booking.new
booking.name = "Spicy food!"
booking.order = DeliveryOrder.new(notes: 'need it really spicy', delivery_time: 'in 2 hours')
booking.save
binding.pry
p Booking.count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment