Coder Social home page Coder Social logo

phase-3-active-record-crud-operations-lab's Introduction

Active Record CRUD Operations Lab

Learning Goals

  • Perform CRUD operations on a SQL database table using Active Record

Introduction

The goal of this lab is to get comfortable performing CRUD (Create, Read, Update, Delete) actions using Active Record.

There are different ways to solve this lab so feel free to experiment!

Setup

First, run bundle install to install the dependencies from the Gemfile.

Next, run bundle exec rake -T, which will list all of the rake tasks you have available in this lab. These tasks come with the sinatra-activerecord gem.

Start the lab by running learn test or keep reading for more instructions.

Create Table

Try using a Rake task to create your migration file:

$ bundle exec rake db:create_migration NAME=create_movies

Once you have a migration file, add columns like so:

Column Name Type
title (string)
release_date (integer)
director (string)
lead (string)
in_theaters (boolean)

After your migration is ready, run both these commands:

$ bundle exec rake db:migrate
$ bundle exec rake db:migrate RAKE_ENV=test

This will migrate your development database as well as a test database so you will be able to run learn test.

You can also run this command to generate some sample data:

$ bundle exec rake db:seed

This will run the code in the db/seeds.rb file in order to create some movies.

Then, if you want to try out your code in the console, run:

$ bundle exec rake console

Use the console to explore various Active Record methods that you'll need in order to pass the tests.

Instructions

Run learn test to see the tests. You'll be adding new methods to the Movie class in app/models/movie.rb that take advantage of Active Record's build-in functionality.

The goal of this is to add some functionality to your Movie class while taking advantage of Active Record's built-in methods. For example, you might write a method Movie.find_all_movies_by_year that finds all the movies for a given year:

class Movie < ActiveRecord::Base
  def self.find_all_movies_by_year(year)
    Movie.where(year: year)
  end
end

.where is a built-in Active Record method that queries the database and returns a list of all items that match the criteria being passed as an argument.

Each test will take us through performing a basic CRUD action using the database we just created. You'll need to refer to the Active Record Query Interface documentation to find the best methods to use.

Note: Pay attention to which methods are instance methods (#) and which are class methods (.).

Create

  • Movie.create_with_title: instantiates a movie with a given title, and saves it to the database

Read

  • Movie.first_movie: returns the first item in the table
  • Movie.last_movie: returns the last item in the table
  • Movie.movie_count: returns the number of records in the table
  • Movie.find_movie_with_id: returns a movie from the table based on its id with the .find method
  • Movie.find_movie_with_attributes: returns a movie from the table based on its attributes with the .find_by method
  • Movie.find_movies_after_2002: uses a .where clause to select the appropriate movies released after 2002

Update

  • Movie#update_with_attributes: updates a single movie using the #update method
  • Movie.update_all_titles: updates the title of all records at once using the .update method

Delete

  • Movie.delete_by_id: deletes a single item with the #destroy method
  • Movie.delete_all_movies: deletes all items at once with the .destroy_all method

Resources

phase-3-active-record-crud-operations-lab's People

Contributors

ihollander avatar maxwellbenton avatar pletcher avatar benjagross avatar deniznida avatar victhevenot avatar annjohn avatar sgharms avatar curiositypaths avatar gj avatar ahimmelstoss avatar lizbur10 avatar johann avatar graciemcguire avatar aspenjames avatar aviflombaum avatar fs-lms-test-bot avatar bhollan avatar irmiller22 avatar azemoh avatar joshua-miles avatar schuylermaclay avatar sarogers avatar fislabstest avatar timothylevi 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.