CHIPS 4.3: ActiveRecord Basics
Practice with ActiveRecord β Railsβ object-relational mapper β in isolation, before you meet the rest of Rails. This is part of Module 4.
The full, step-by-step assignment lives in the starter repo,
saasbook/hw-activerecord-practice:
start with its README.md.
You write your queries in lib/activerecord_practice.rb and check them against
the provided examples in spec/activerecord_practice_spec.rb.
Goals
- Understand how ActiveRecord maps Ruby classes to database tables and objects to rows.
- Perform the basic CRUD lifecycle (create, read, update, delete) from Ruby.
- Compose queries with
where,find, ordering, and simple scopes.
What youβll do
Thirteen short exercises against a standalone (non-Rails) ActiveRecord setup with a database of 30 fake customers, so you can focus purely on the model layer: writing queries and manipulating records to satisfy the provided tests.
Before you start
Watch Module 4 Β§4.1β4.2 and read the matching textbook sections. If you have never worked with relational databases, the starter repoβs README points you at a couple of free SQL primers β skim one first.
Logistics
- Getting the code: see Getting the Code below β you clone the public starter repo and push it to a private repo of your own.
- Autograding: submit to Gradescope; you may submit as many times as you like before the deadline. The autograder is a way for you to check your work and your understanding. Run the tests locally first β see Running Code and Tests.
- Extensions: everyone automatically gets a 2-day no-penalty extension; see the extension policy.
- Collaboration: you may collaborate with your team of 4, and discuss high-level approaches with anyone. Sharing code outside your team is academic misconduct. See the AI Use Policy below before using AI tools.
AI Use Policy
This assignment follows the course AI Policy, which will be published on this site before it is needed. Until then, the rule from the Syllabus applies: you must be able to explain everything you submit.
Getting the Code
Public starter repo: saasbook/hw-activerecord-practice —
https://github.com/saasbook/hw-activerecord-practice
GitHub makes it challenging to have a private fork of a public repository. These steps allow you to create a private space for you own work but still update from the starter code.
Clone the starter repo, then use gh
(see Local Development)
to create a private copy under your own GitHub account and
push the code to it. Replace YOUR_GITHUB_USERNAME with your
GitHub username:
git clone https://github.com/saasbook/hw-activerecord-practice.git
cd hw-activerecord-practice
git remote rename origin upstream
gh repo create YOUR_GITHUB_USERNAME/hw-activerecord-practice --private --source=. --remote=origin --push
git remote set-url --push upstream DISABLED
After this, origin is your private repo and upstream
is the public starter repo. The last command disables pushing to
upstream, so git push always goes to your own
copy β never to the public starter repo.
Keeping your repo up to date
Your private repo is a snapshot: it does not follow the starter
repo. If we fix or add something in saasbook/hw-activerecord-practice
after you copy it, you have to pull those changes in yourself. The
upstream remote you set up above is how you do that:
git fetch upstream
git fetch only downloads β it changes nothing in your working
copy. Review what you are about to take before merging it:
git diff --name-only HEAD upstream/main # just the names of the files that differ
git diff --stat HEAD upstream/main # the same list, plus how much changed in each
git diff HEAD upstream/main -- spec # the actual changes, limited to one file or directoryThen merge the upstream changes into your branch and push them to your private repo:
git merge upstream/main
git push origin main
If you and we edited the same lines, git stops and reports a conflict: open
each file it lists, keep the correct combination of both sides, delete the
<<<<<<< / ======= /
>>>>>>> markers, then git add
those files and git commit. Ask on Ed if a merge leaves you
stuck β don't start over and lose your work.
These commands assume the starter repo's default branch is main.
If git branch -r shows upstream/master instead, use
that name in the commands above.
View this assignment on bCourses.
Running Code and Tests
Run everything below from the root of your clone (see Getting the Code). If Ruby isn't installed yet, work through Local Development first.
Install the dependencies
bundle install
Run this once after cloning, and again whenever the Gemfile
changes. Prefix the commands below with bundle exec so you run
the same gem versions the autograder does.
Boot the project
This assignment is a plain Ruby library, so there is no server to boot. Run a file directly, or load your code into an interactive console and call methods on it by hand:
bundle exec ruby lib/YOUR_FILE.rb
bundle exec irb -I lib -r YOUR_FILERun the tests
Run the RSpec suite the way the Gradescope autograder will β get it green locally before you submit:
bundle exec rspec # the whole suite
bundle exec rspec spec/YOUR_FILE_spec.rb # one spec file
bundle exec rspec spec/YOUR_FILE_spec.rb:42 # one example, by line number
bundle exec rspec --format documentation # every example's descriptionEach failure prints the file and line of the failing example along with what it expected β start there rather than at the top of the output.
Run the tests continuously
The starter repo ships a Guardfile: start Guard once and it re-runs the
specs every time you save a file, so you can stay in a code-and-test groove.
bundle exec guard
Every example in the specfile starts out as xspecify, which tells RSpec to
skip it. Change one to specify when you start working on that query, so you
only see the failure you are currently trying to fix.