Code Monkey home page Code Monkey logo

activerecord_crud's Introduction

Steps to setup Ruby app with activerecord

  1. In root of project/app, run bundle init
  2. Add gems: activerecord, pry, sinatra, sintra-activerecord, require_all, run bundle install
  3. Create config and lib directories
  4. Create config/environment.rb
  5. mkdir lib/models
  6. Touch Rakefile - require ‘sinatra/activerecord/rake’ and ‘config/environment.rb’
  7. In config/environment.rb, require ‘sinatra/activerecord’ and ‘require_all’, then do ‘require_all ‘lib’’, establish ActiveRecord::Base connection to db (call database whatever the name of the db is)
ActiveRecord::Base.establish_connection(
  :adapter => 'sqlite3',
  :database => 'db/cats.db'
)
  1. Run rake -T to make sure we have access to raketasks
  2. Run rake db:create_migration NAME=create_cats_table (will create the db folder if it doesn’t already exist) and will add the migration file to db/migration
  3. Write migration file, then run rake db:migrate
  4. Then can see schema in file structure, can also drop into sqlite3 cats.db to see the tables and schema, but don’t really need to do that anymore. Review rollback here
  5. Create seeds in db/seeds.rb and run rake db:seed
  6. Now can put a pry in environment.rb to run Breed.all and see your seeds.

Notes

  • To view database, can run sqlite3 db/cats.db, then can run .schema or .tables and can run any SQL commands. (Don't need to do this anymore though! ActiveRecord gives us a schema file!)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.