{"id":13493023,"url":"https://github.com/sinatra-activerecord/sinatra-activerecord","last_synced_at":"2025-03-28T11:31:19.513Z","repository":{"id":3351473,"uuid":"4396535","full_name":"sinatra-activerecord/sinatra-activerecord","owner":"sinatra-activerecord","description":"Extends Sinatra with ActiveRecord helper methods and Rake tasks.","archived":false,"fork":true,"pushed_at":"2023-10-23T07:56:32.000Z","size":237,"stargazers_count":729,"open_issues_count":7,"forks_count":113,"subscribers_count":13,"default_branch":"master","last_synced_at":"2024-05-18T21:36:10.669Z","etag":null,"topics":["activerecord","sinatra"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"bmizerany/sinatra-activerecord","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sinatra-activerecord.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-05-21T17:51:38.000Z","updated_at":"2024-03-25T13:26:11.000Z","dependencies_parsed_at":"2023-01-16T18:31:36.212Z","dependency_job_id":null,"html_url":"https://github.com/sinatra-activerecord/sinatra-activerecord","commit_stats":null,"previous_names":["janko-m/sinatra-activerecord"],"tags_count":37,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinatra-activerecord%2Fsinatra-activerecord","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinatra-activerecord%2Fsinatra-activerecord/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinatra-activerecord%2Fsinatra-activerecord/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinatra-activerecord%2Fsinatra-activerecord/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sinatra-activerecord","download_url":"https://codeload.github.com/sinatra-activerecord/sinatra-activerecord/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246020880,"owners_count":20710836,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activerecord","sinatra"],"created_at":"2024-07-31T19:01:11.453Z","updated_at":"2025-03-28T11:31:19.216Z","avatar_url":"https://github.com/sinatra-activerecord.png","language":"Ruby","funding_links":[],"categories":["ORM","Ruby"],"sub_categories":[],"readme":"# Sinatra ActiveRecord Extension\n\nExtends [Sinatra](http://www.sinatrarb.com/) with extension methods and Rake\ntasks for dealing with an SQL database using the\n[ActiveRecord ORM](https://github.com/rails/rails/tree/master/activerecord).\n\n![test badge](https://github.com/sinatra-activerecord/sinatra-activerecord/workflows/rspec/badge.svg)\n\n## Requirement\nActiveRecord \u003e= 4.1\n\n## Setup\n\nPut it in your `Gemfile`, along with the adapter of your database. For\nsimplicity, let's assume you're using SQLite:\n\n```ruby\ngem \"sinatra-activerecord\"\ngem \"sqlite3\"\ngem \"rake\"\n```\n\nNow require it in your Sinatra application, and establish the database\nconnection:\n\n```ruby\n# app.rb\nrequire \"sinatra/activerecord\"\n\nset :database, {adapter: \"sqlite3\", database: \"foo.sqlite3\"}\n# or set :database_file, \"path/to/database.yml\"\n```\n\nIf you have a `config/database.yml`, it will automatically be loaded, no need\nto specify it. Also, in production, the `$DATABASE_URL` environment variable\nwill automatically be read as the database (if you haven't specified otherwise).\n\nIf both `config/database.yml` and `$DATABASE_URL` are present, the database configuration of this two will be merged, with $DATABASE_URL's variable taking precedence over database.yml (for the same variable / key).\n\n\nNote: If you are using ActiveRecord 6.0 and above, and have [defined multiple databases](https://guides.rubyonrails.org/active_record_multiple_databases.html#setting-up-your-application) for the database.yml, the $DATABASE_URL configuration will be discarded, following [Active Record convention here](https://github.com/rails/rails/blob/main/activerecord/lib/active_record/database_configurations.rb#L169).\n\n\nNote that in **modular** Sinatra applications you will need to first register\nthe extension:\n\n```ruby\nclass YourApplication \u003c Sinatra::Base\n  register Sinatra::ActiveRecordExtension\nend\n```\n\nNow require the rake tasks and your app in your `Rakefile`:\n\n```ruby\n# Rakefile\nrequire \"sinatra/activerecord/rake\"\n\nnamespace :db do\n  task :load_config do\n    require \"./app\"\n  end\nend\n```\n\nIn the Terminal test that it works:\n\n```sh\n$ bundle exec rake -T\nrake db:create            # Create the database from DATABASE_URL or config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config)\nrake db:create_migration  # Create a migration (parameters: NAME, VERSION)\nrake db:drop              # Drops the database using DATABASE_URL or the current Rails.env (use db:drop:all to drop all databases)\nrake db:fixtures:load     # Load fixtures into the current environment's database\nrake db:migrate           # Migrate the database (options: VERSION=x, VERBOSE=false)\nrake db:migrate:status    # Display status of migrations\nrake db:rollback          # Rolls the schema back to the previous version (specify steps w/ STEP=n)\nrake db:schema:dump       # Create a db/schema.rb file that can be portably used against any DB supported by AR\nrake db:schema:load       # Load a schema.rb file into the database\nrake db:seed              # Load the seed data from db/seeds.rb\nrake db:setup             # Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first)\nrake db:structure:dump    # Dump the database structure to db/structure.sql\nrake db:version           # Retrieves the current schema version number\n```\n\nAnd that's it, you're all set :)\n\n## Usage\n\nYou can create a migration:\n\n```sh\n$ bundle exec rake db:create_migration NAME=create_users\n```\n\nThis will create a migration file in your migrations directory (`./db/migrate`\nby default), ready for editing.\n\n```ruby\nclass CreateUsers \u003c ActiveRecord::Migration\n  def change\n    create_table :users do |t|\n      t.string :name\n    end\n  end\nend\n```\n\nNow migrate the database:\n\n```sh\n$ bundle exec rake db:migrate\n```\n\nYou can also write models:\n\n```ruby\nclass User \u003c ActiveRecord::Base\n  validates_presence_of :name\nend\n```\n\nYou can put your models anywhere you want, only remember to require them if\nthey're in a separate file, and that they're loaded after `require \"sinatra/activerecord\"`.\n\nNow everything just works:\n\n```ruby\nget '/users' do\n  @users = User.all\n  erb :index\nend\n\nget '/users/:id' do\n  @user = User.find(params[:id])\n  erb :show\nend\n```\n\nA nice thing is that the `ActiveRecord::Base` class is available to\nyou through the `database` variable:\n\n```ruby\nif database.table_exists?('users')\n  # Do stuff\nelse\n  raise \"The table 'users' doesn't exist.\"\nend\n```\n\n## History\n\nThis gem was made in 2009 by Blake Mizerany, creator of Sinatra.\n\n## Social\n\nYou can follow Janko on Twitter, [@jankomarohnic](http://twitter.com/jankomarohnic).\nYou can follow Axel on Twitter, [@soulchildpls](http://twitter.com/soulchildpls).\n\n## License\n\n[MIT](https://github.com/sinatra-activerecord/sinatra-activerecord/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsinatra-activerecord%2Fsinatra-activerecord","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsinatra-activerecord%2Fsinatra-activerecord","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsinatra-activerecord%2Fsinatra-activerecord/lists"}