Hi guys. I have a question regarding database and rendering the data
I have a table called ‘vehicles’
id
| type
| car_id
| motorcycle_id
| truck_id
defmodule Hello.Repo.Migrations.CreateVehicle do
use Ecto.Migration
def change do
create table(:vehicles) do
add :type, int
add :car_id, references(:cars)
add :motorcycle_id, references(:motorcycles)
add :truck_id, references(:trucks)
end
end
end
Car, motorcycle and truck have different properties
But it does not seems to be a good way to do it
How can I make the table to only have a type_id
where it will show the id
for each type of car or motorcycle or truck and how to render it?
Json example:
[ { "id":1, "type":"car", "car":{ "brand":"Toyota", "number_of_seat":4 } }, { "id":1, "type":"motorcycle", "motorcycle":{ "brand":"Yamaha", "some_property":2 } }, { "id":1, "type":"truck", "truck":{ "brand":"Truck brand", "load_capacity":10000 } } ]