Giter Club home page Giter Club logo

boiler_plate_ar_setup's Introduction

Mod 1 ActiveRecord Starter Repo

In config/database.yml, you can change the name of the database from db/cats.sqlite3 to whatever reflects your project. For example: db/notes.sqlite3. Doesn't really matter what you call the db.

ActiveRecord Methods

These common ActiveRecord methods will help you interact with your database. Please refer to the ActiveRecord docs for what they do and how to use them. (If the docs talk about Rails, that is ok! ActiveRecord works very similar with or without Rails.)

  .create (.new, .save)
  .all
  .count
  .find
  .find_by
  .where

Notes

Remember, any model that will have a corresponding table in the database needs to inherit from ActiveRecord::Base ex:

class Cat < ActiveRecord::Base
  # customer methods defined here
end
  • To view database, you 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!)

Steps to setup Ruby app with activerecord

(New for ActiveRecord 6.0)

The following steps are already done for you in this boiler plate repo.

The steps below are provided to you as a reference only.

You're not expected to memorize this (please don't).

  1. In root of project, run bundle init
  2. Add gems: bundle add activerecord pry sinatra, sinatra-activerecord rake sqlite3 require_all run bundle install
  3. mkdir config and lib
  4. mkdir lib/models
  5. touch config/environment.rb config/database.yml
  6. Create your model files and models (make sure to have your models inherit from ActiveRecord::Base)
  7. In config/environment.rb:
  require 'bundler/setup'
  Bundler.require

  require_all 'lib'
  1. In config/database.yml:
development:
  adapter: sqlite3
  database: db/cats.sqlite3
  1. Touch Rakefile - require ‘config/environment.rb’ and require_relative ‘sinatra/activerecord/rake’
  2. Run rake -T to make sure we have access to raketasks
  3. 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
  4. Write migration file, then run rake db:migrate
  5. 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
  6. Create seeds in db/seeds.rb and run rake db:seed
  7. Now can put a pry in environment.rb to run .all and see your seeds.

Make sure your models inherit from ActiveRecord::Base

boiler_plate_ar_setup's People

Contributors

kathleenscriver avatar

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.