Created
August 9, 2024 19:58
-
-
Save toddsiegel/9be4ba387c30674a3ce733d5f6cef379 to your computer and use it in GitHub Desktop.
One file ActiveRecord script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/inline' | |
gemfile(true) do | |
source 'https://rubygems.org' | |
gem 'sqlite3', '~> 1.4' | |
gem 'activerecord', '7.1.3.4' | |
end | |
require 'sqlite3' | |
require 'active_record' | |
ActiveRecord::Base.establish_connection( | |
adapter: 'sqlite3', | |
database: ':memory:' | |
) | |
ActiveRecord::Schema.define do | |
# Create table here, e.g. | |
# create_table :users, force: true do |t| | |
# t.string :name, null: false | |
# end | |
# | |
# create_table :posts, force: true do |t| | |
# t.belongs_to :user, index: true, null: false | |
# t.string :title, null: false | |
# end | |
end | |
# Define models here, e.g. | |
# class User < ActiveRecord::Base | |
# has_many :posts | |
# end | |
# | |
# class Post < ActiveRecord::Base | |
# belongs_to :user | |
# end | |
# Run code here... | |
# user = User.create!(name: 'User 1') | |
# user.posts.create!(title: 'Post 1') | |
# user.posts.create!(title: 'Post 2') | |
# | |
# puts "user.posts.count: #{user.posts.count}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment