Last active
May 15, 2025 18:22
-
-
Save pgwillia/d1ded56f1b1ca4917b5865a10e08916d to your computer and use it in GitHub Desktop.
Use ActiveRecord + SQLite without Rails (originally https://subinsb.com/ruby-activerecord-sqlite/)
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
| source "https://rubygems.org" | |
| gem "activerecord", "~> 8.0" | |
| gem "pry" | |
| gem "sqlite3", "~> 2.4" |
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 'active_record' | |
| require 'pry' | |
| require 'sqlite3' | |
| ActiveRecord::Base.establish_connection( | |
| adapter: 'sqlite3', | |
| database: 'db.sqlite3' | |
| ) | |
| class Place < ActiveRecord::Base | |
| end | |
| @first_run = !Place.table_exists? | |
| # Create the table (migration-like setup) | |
| if @first_run | |
| ActiveRecord::Schema.define do | |
| create_table :places do |t| | |
| t.string :name | |
| t.string :lat | |
| t.string :lon | |
| t.timestamps | |
| end | |
| add_index :places, :name, unique: true | |
| end | |
| end | |
| binding.pry |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment