This is a Rails app for the owner of a tow lot to keep track of tow trucks and towed vehicles.
- Fork the repo!
- Create a new gemset for
$ bundle install
$ rake db:setup # create the database and load this data
$ rails console
Tow trucks
have a make
, model
, year
, an acquisition date
, the date when it was last serviced
, the mileage
and a text field for notes
.
create_table "tow_trucks", force: true do |t|
t.text "make"
t.text "model"
t.integer "year"
t.date "acquired_at"
t.date "last_service_at"
t.integer "mileage"
t.text "notes"
end
Vehicles
have a make
, model
, and color
(text), year
(integer), VIN
(unique non-nullable indexed text), a category
(car/motorcycle/truck?), an acquisition date
, a release date
, a boolean indicating whether the towing fee has been paid
and a text field for notes
.
create_table "vehicles", force: true do |t|
t.text "make"
t.text "model"
t.text "color"
t.integer "year"
t.text "vin", null: false
t.text "category", null: false
t.date "acquired_at"
t.date "released_at"
t.decimal "fee", precision: 8, scale: 2
t.boolean "is_paid"
t.text "notes"
end
The owner of the Tow Lot app has some questions about the data that is now entered into it.
Write a single ActiveRecord query to achieve each of the following objectives. Record your finished queries in a separate file.
Note: "Vehicle" here only refers to records in the vehicles
table (i.e. not tow trucks).
- Get a list of all tow trucks.
- Get the vehicle with ID 3.
- Get the vehicle with VIN D0985DF1593A350A4.
- Get a list of all vehicles sorted by acquisition date.
- Get a list of all silver vehicles, ordered alphabetically by make.
- Get a list of all red Honda vehicles that are cars.
- Get a count of all vehicles that are motorcycles.
- Get a count of vehicles that are currently on the lot (i.e. not released).
- Get a list of all tow trucks whose mileage is over 200,000.
- Get a list of all tow trucks whose last service was more than 8 months ago.
- Get the average tow fee across all vehicles.
- Get the top 3 vehicles with the highest tow fees.
- Get all vehicles that have a fee assessed which has not been paid.
- Get a list of all released vehicles, sorted by most recently released.
- Get a list of all vehicles that have notes.
- Get the VINs of all vehicles manufactured before the year 2000.
- Get the make, model, and year of all silver vehicles, ordered by year.
- Get the total income from all vehicles (i.e. sum of all fees that are paid).
The motorcycle business is going well, so the owner is frequently sorting and filtering by vehicle category. Add an index on this column to make these operations faster, and make it non-nullable to ensure all vehicles are categorized.