GithubHelp home page GithubHelp logo

activerecord-crud's Introduction

Active Record CRUD

Objective

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!

Instructions

Before starting this lab run bundle, to get the proper gem dependencies. If your operating system is OSX El Capitan, and you have an issue installing EventMachine, first check to make sure Open SSL is installed by entering brew install openssl in terminal. Once it's installed, enter brew link openssl --force.

Also enter 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 or keep reading for more instructions.

Create Table

Try using the rake task rake db:create_migration NAME=create_movies to create your migration file. 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 rake db:migrate to migrate your table and rake db:migrate SINATRA_ENV=test to migrate a test database so you will be able to run learn

Tests

Run rspec or learn to see the tests. To make them pass open movie_controller.rb and complete each method. It will help to open spec/models/movie_spec.rb to see exactly what each spec is testing for.

In each method the __ corresponds to a line of code you will need to write to make the spec pass.

Each test will take us through performing a basic CRUD action using the database we just created. These tests will take you through:

Create

  • A movie can be instantiated, given a title, and saved
  • A movie can be instantiated with a hash containing all of its attributes
  • A movie can be created in a block

Read

  • You can return the first item in the table
  • You can return the last item in the table
  • You can return the number of records in the table
  • You can return a movie from the table based on its attributes
  • You can use a where clause to select the appropriate movies and sort them by release date

Update

  • Can be found, updated, and saved
  • Can be updated using the update method
  • Can update all records at once

Destroy

  • Can destroy a single item
  • Can destroy all items at once

Resources

Active Record Query Interface.

View Active Record CRUD on Learn.co and start learning to code for free.

activerecord-crud's People

Contributors

ahimmelstoss avatar annjohn avatar aspenjames avatar aviflombaum avatar azemoh avatar benjagross avatar bhollan avatar curiositypaths avatar deniznida avatar dependabot[bot] avatar fislabstest avatar fs-lms-test-bot avatar gj avatar irmiller22 avatar johann avatar joshua-miles avatar maxwellbenton avatar pletcher avatar rrcobb avatar sarogers avatar schuylermaclay avatar sgharms avatar victhevenot avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

activerecord-crud's Issues

possible type in movie_spec.rb

I think that line 109 in movie_spec.rb should have an argument name of can_find_by_different_attributes so that there isn't an undefined method error.

No Read Me or spec folder with tests

There are currently no specs/tests to run. There is also no ReadMe as it looks like the readme was removed a few days ago and not replaced. As of now, when the lab page on Learn.co loads, it is completely empty, and there are no tests to pass or even work towards.

the syntax error

I got a notification that the starter code in this lab was fixed so as not to throw a syntax error anymore. However, a student just popped up on AAQ with the same issue. The underscores between the {} are causing it.

I think there is a test that needs to be fixed

in Movie_spec.rb: The current test passes by default (without using the method being tested.

it 'can be updated using #update' do
        Movie.create(title: "Wat?")
        movie = Movie.find_by(title: "Wat?")
        movie.update(title: "Wat, huh?")
        expect(Movie.find_by(title: "Wat, huh?")).to_not be_nil
      end

Can be changed to:

it 'can be updated using #update' do
        can_update_using_update_method
        expect(Movie.find_by(title: "Wat, huh?")).to_not be_nil
      end

@StevenNunez @SophieDeBenedetto @antoinfive

database_cleaner gem issue

There appears to be an issue with the database_cleaner gem, which is causing students to be unable to run the learn command. Screen shot of the error is provided.

Screen Shot 2019-05-23 at 4 06 21 PM

Explain the Take Away of the Lab

The spec file here is pretty magical and important. The point of the lab is to recognize all the sort of methods that AR provides on a convention-oriented class (that wraps a DB table). You get accessors for each column. CRUD methods. Finders. Etc. This lab should either tell the student or force the student somehow to recognize the common AR interface.

The Wachowski Brothers are now the Wachowski Sisters

noticed that in the spec, Movie.new tests for the Matrix movie and lists movie.director = "The Wachowski Brothers"

Both Wachowskis have transitioned to female and are now the Wachowskis or the Wachowski Sisters.

Lab starts out with syntax errors

There are blanks for students to fill in denoted by "__" in several spots. The ones that appear in between the curly braces are causing a syntax error.

Problem running rspec test

One student got this error when trying to run bundle install (needed to run rspec test):

An error occurred while installing json (1.8.3), and Bundler cannot continue.
Make sure that `gem install json -v '1.8.3' --source 'https://rubygems.org/'` succeeds before bundling.

In Gemfile:
sinatra-activerecord was resolved to 2.0.9, which depends on
activerecord was resolved to 4.2.6, which depends on
activemodel was resolved to 4.2.6, which depends on
activesupport was resolved to 4.2.6, which depends on
json

Running 'gem install json -v '1.8.3' did not work and threw error messages. Solution below:

$ bundle update json
$ bundle install

After this rspec could run.

Should this lab be placed later in the section?

It appears this lab may have been intended to be placed later in the ActiveRecord section, as it requires that the student write migrations, despite there being no migration curriculum material inside of or before this lab (there are three lessons involving migrations that follow it).

JSON gem issues

The json (1.8.3) in the Gemfile.lock causes issues. Deleting Gemfile.lock then running bundle resolves the install issue.

It gaves error without any line of code

This lab doesn't work, It gave error when i run learn before typing any line of code. I asked a question and tried with Coach but he has same error.
The error is

D, [2019-08-17T11:47:25.299971 #11258] DEBUG -- :    (1.3ms)  SELECT sqlite_version(*)

An error occurred while loading spec_helper.
Failure/Error: raise 'Migrations are pending. Run `rake db:migrate SINATRA_ENV=test` to resolve the issue.'

RuntimeError:
  Migrations are pending. Run `rake db:migrate SINATRA_ENV=test` to resolve theissue.
# ./spec/spec_helper.rb:8:in `<top (required)>'


Finished in 0.00171 seconds (files took 1.46 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

Finished in 0.00171 seconds (files took 1.46 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

When I tried rake db:migrate SINATRA_ENV=test
it gave me

rake db:migrate SINATRA_ENV=test
rake aborted!
LoadError: cannot load such file -- sinatra/activerecord/rake/activerecord_6
/usr/local/rvm/gems/ruby-2.6.1/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:325:in `require'
/usr/local/rvm/gems/ruby-2.6.1/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:325:in `block in require'
/usr/local/rvm/gems/ruby-2.6.1/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:291:in `load_dependency'
/usr/local/rvm/gems/ruby-2.6.1/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:325:in `require'
/usr/local/rvm/gems/ruby-2.6.1/gems/sinatra-activerecord-2.0.13/lib/sinatra/activerecord/rake.rb:2:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.6.1/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:325:in `require'
/usr/local/rvm/gems/ruby-2.6.1/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:325:in `block in require'
/usr/local/rvm/gems/ruby-2.6.1/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:291:in `load_dependency'
/usr/local/rvm/gems/ruby-2.6.1/gems/activesupport-6.0.0/lib/active_support/dependencies.rb:325:in `require'
/home/proactive-karma-6410/activerecord-crud-online-web-ft-071519/Rakefile:4:in`<top (required)>'
/usr/local/rvm/gems/ruby-2.6.1/gems/rake-12.3.3/exe/rake:27:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.6.1/bin/ruby_executable_hooks:24:in `eval'
/usr/local/rvm/gems/ruby-2.6.1/bin/ruby_executable_hooks:24:in `<main>'
(See full trace by running task with --trace)

more than one line of code

In each method the __ corresponds to a line of code you will need to write to make the spec pass
There are different ways to solve this lab so feel free to experiment!
"You can use more than one line of code" - Justin Daniel, Technical Coach

Incorrect Method name causes Rspec Error

The snippet below is from movies_controller.rb. It fails the test, but only because the given method name is incorrect. Upon changing the word "different", to "multiple". The tests pass. If that was to intentionally challenge us, then right on! It made me pay attention to detail and examine the error message closely.

def can_find_by_different_attributes
  # title == "Title"
  # release_date == 2000, 
  # director == "Me"
  Movie.find_by(title: "Title", release_date: 2000, director: "Me")
end

Typo between method / spec file, and one method doesn't seem to test

  1. method is named different than spec file:
    FAILS:
    def can_find_by_different_attributes

    title == "Title"

    release_date == 2000,

    director == "Me"

    Movie.find_by(title: "Title")
    end

PASSES:
def can_find_by_multiple_attributes

title == "Title"

release_date == 2000,

director == "Me"

Movie.find_by(title: "Title")
end

  1. a)This one doesn’t seem to factor into the tests. Code does not affect pass/fail count.
    b)Typo: Updtate
    def can_be_found_updated_and_saved

    Updtate the title "Awesome Flick" to "Even Awesomer Flick"

    Movie.create(title: "Awesome Flick")
    Movie.find(title: "Awesome Flick")
    Movie.update(title: "Even Awesomer Flick")
    Movie.save
    end

Edge case in the spec

When I was doing the spec for 'can_get_size_of_the_database' I was thinking of how to approach it, My first try was to do Movie.last.id which would give the ID of the last movie added which would be the size of the database under normal circumstances.
I then realized that that shouldn't be the correct answer because it won't account for a case where a record was deleted, nevertheless it still passed the test.
You might want to change the test to account for that? Maybe by creating a movie with an ID greater than 5?

macos mojave 10.14.2 trouble installing eventmachine

when running bundle:

An error occurred while installing eventmachine (1.2.7), and Bundler cannot continue. Make sure that gem install eventmachine -v '1.2.7' --source 'https://rubygems.org/'`
succeeds before bundling.`

and when attempting to run learn:

`Bundler could not find compatible versions for gem "eventmachine":
In Gemfile:
thin was resolved to 1.7.2, which depends on
eventmachine (~> 1.0, >= 1.0.4)

Could not find gem 'eventmachine (~> 1.0, >= 1.0.4)', which is required by gem 'thin', in any of the sources.`

brew install openssl returned that openssl was already installed, and running brew link openssl --force didn't resolve the problem.

Clarify the can_be_created_in_a_block method

README instructions say that "In each method the __ corresponds to a line of code you will need to write to make the spec pass."

Add the __ to show that more code should be added.

Line 25 of movies_controller.rb can be updated from ---def can_be_created_in_a_block(args = {})--- to ---def can_be_created_in_a_block(args = { __ })---

That's the area that needs to be updated to pass the 'can be created in a block when no args are passed' on line 72 of movie_spec.rb

fork this lab and remove the migrations stuff

This is a great candidate for just basic AR CRUD stuff.

Fork this lab, and remove all the references to the migrations. Just have it so that they set up the database, then hey look ALL THE MAGIC METHODS.

Lab order incorrect

All the content of this lab isn't discussed until Intro to Rake, which is the lesson after this one.

ambiguous movie_controller.rb method name change

Method names in movie_controller.rb file (below) and the spec file are ambiguous.
They ask to return the first item in the database where as it is expecting the title of the first item in the database.

def can_get_the_first_item_in_the_database
end

def can_get_the_last_item_in_the_database
end

Method names

bundle out of date: eventmachine (1.0.7) to eventmachine (1.2.0.1)

after typed 'bundle install' in command line:
An error occurred while installing eventmachine (1.0.7), and Bundler cannot
continue.
Make sure that gem install eventmachine -v '1.0.7' succeeds before bundling.

so i changed
eventmachine (1.0.7) to eventmachine (1.2.0.1) in Gemfile.lock

and then typed 'gem install eventmachine' ,the bundle worked...

def can_be_created_in_a_block spec issue

def can_be_created_in_a_block
Movie.create do |m|
__
end
end


In this lab, the spec asks for us to create a new instance of movie with a block with the #create method.

We figured out that unless you hardcore what the test is passing:

it 'can be created in a block' do
movie = can_be_created_in_a_block

    expect(Movie.last).to eq(movie)
    expect(Movie.last.title).to eq("Home Alone")
    expect(Movie.last.release_date).to eq(1990)
  end

--
There is no way to pass the spec. It is specifically asks you to solve this problem with one line of code, but based on the fact there are no arguments it is impossible.

--

We have confirmation from JJ that this is true.

:) <3

error in schema.rb

My schema.rb that was auto-generated after migration defined the data types incorrectly for t.string "release_date" and t.string "in_theaters". The data type for "release_date" should be date. The data type for "in_theaters" should be boolean.

After trying for couple hours, I was unable to find a fix.

Thank you

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.