I want you to build a Rails API that helps a logistics company to manage their fleet of vehicles.
The company has many depots around the UK, and each depot has a set of vehicles that serve the depot.
You will be responsible for modelling the entities and their relationships above.
To start off, it's probably best to do some research about the underlying database tables and their links to one another. Usually you would create something called a ERD (entity relationship diagram) to model your database tables prior to actually jumping into coding the Rails models that generate the database tables. There are many tools for diagramming online that you can use to do this for free.
I want you to use Rails models to represent each of the entities below. The entities and their properties are detailed below:
The company, in this case, is called Sam Incorporated
:
- It has many depots
- It has a name e.g.
Sam Incorporated
A depot has:
- Many vehicles attached to it.
- It's location e.g. Bristol
location
is required, and there must be at least one vehicle attached to each depot.
A vehicle:
- Belongs to only one
depot
- Has a registration number e.g AD55 GS0
- Has a make e.g. Vauxhall
- Has a model e.g Astra
- Has a mileage e.g. 30000 miles
make
, model
and mileage
, registration_number
are required fields
I want you to create two new endpoints which will allow:
- Creation of a new vehicle within a particular depot. This will be a HTTP
POST
request. The path should beapi/depots/{depot_id}/vehicles
- Retrieve a list of vehicles for a particular depot. This will be a HTTP
GET
request. The path should beapi/depots/{depot_id}/vehicles
The JSON response payload returned for both endpoints and the request payload (for the POST
) is up to you. But when you are creating a vehicle, the endpoint must allow you to create a vehicle and set all of its required fields.
Check you have a recent version of Rails (5 or 6):
rails -v
You will need to create a brand new Ruby on Rails application from scratch. I want you to create an API only
Rails app (e.g with no views) and I want you to use postgresql
as the database.
You can do this by running:
rails new sams-company --api --database=postgresql
Beyond this, you are on your own (although obviously I will help when you get stuck).
I would expect you to add unit tests for the endpoints, and also for all of the models that you have created to test any non trivial functionality.