Skip to content

Instantly share code, notes, and snippets.

@kaspth
Created April 25, 2024 17:16
Show Gist options
  • Save kaspth/4b9702f2b803a32c6644fa0f522907ac to your computer and use it in GitHub Desktop.
Save kaspth/4b9702f2b803a32c6644fa0f522907ac to your computer and use it in GitHub Desktop.
require "bundler/inline"
gemfile do
gem "rails"
gem "sqlite3"
end
require "active_record"
require "action_controller"
require "rails"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Schema.define do
create_table :posts do |t|
t.references :user, null: false, index: true
t.string :title, null: false
t.text :content, null: false
end
create_table :users do |t|
end
end
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
class Post < ApplicationRecord
belongs_to :user
end
class User < ApplicationRecord
has_many :posts
end
user = User.create!
post = Post.create! user:, title: "First", content: "Heyo"
class UsersController < ActionController::Base
def show
@user = User.first
render json: @user
end
end
class App < Rails::Application
routes.default_url_options[:host] = 'localhost:3000'
config.logger = Logger.new(STDOUT)
routes.append do
resources :users
end
end
App.initialize!
require "rackup"
Rackup::Server.start(app: Rails.application, Port: 3000)
# binding.irb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment