Skip to content

Commit

Permalink
Migraciones, seeds y README
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmanuelramallo committed Aug 19, 2022
1 parent 335dfed commit c78bbeb
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 0 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# LAB2

## Objetivos

Familiarizarse con los modelos y sus características.

## Notas
- Las tablas ya existen y tienen datos
- Usar `reload!` en la consola de rails para aplicar cualquier cambio hecho en el código fuente

## Pasos previos

Ver la sección [preparar repositorio](https://github.com/I110IS/lab1/blob/master/README.md#preparar-repositorio)

## Parte 1 - [Validaciones](https://guides.rubyonrails.org/active_record_validations.html)

1. Definir los modelos para las tablas `monsters`, `victims` y `attacks`.
1. ¿Cuántos monstruos hay en la base de datos? (usar la consola de rails)
1. Definir una validación para que el nombre de los monstruos y las víctimas sea obligatorio.
1. Definir una validación para que el nombre de los monstruos no se puedan repetir entre monstruos.
1. Definir una validación custom para que los monstruos con un nivel de susto superior a 5 no puedan tener víctimas menores de 18 años.
1. [rails console] Probar que todas validaciones funcionen como se esperan.
1. Hacer un commit con todos los cambios realizados y pushear el commit al repositorio forkeado.

## Parte 2 - [Asociaciones](https://guides.rubyonrails.org/association_basics.html)

1. Definir las asociaciones necesarias para que un monstruo pueda tener muchas víctimas. Es necesario considerar el modelo de ataques.
1. [rails console] Crear una nueva víctima y asociarla a un monstruo
1. [rails console] Obtener todas las víctimas de Nahuelito
1. [rails console] Obtener todas las víctimas de Nahuelito ordenadas alfabéticamente por su nombre
1. Actualizar los modelos para los monstruos y las víctimas de tal manera que cuando se elimine algún monstruo o víctima, todos sus ataques se eliminen.
1. Hacer un commit con todos los cambios realizados y pushear el commit al repositorio forkeado.
11 changes: 11 additions & 0 deletions db/migrate/20220819004043_create_monsters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateMonsters < ActiveRecord::Migration[7.0]
def change
create_table :monsters do |t|
t.string :name
t.text :description
t.integer :scare_level

t.timestamps
end
end
end
10 changes: 10 additions & 0 deletions db/migrate/20220819004219_create_victims.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateVictims < ActiveRecord::Migration[7.0]
def change
create_table :victims do |t|
t.string :name
t.date :birthdate

t.timestamps
end
end
end
10 changes: 10 additions & 0 deletions db/migrate/20220819004303_create_attacks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateAttacks < ActiveRecord::Migration[7.0]
def change
create_table :attacks do |t|
t.references :monster, null: false, foreign_key: true
t.references :victim, null: false, foreign_key: true

t.timestamps
end
end
end
43 changes: 43 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
ApplicationRecord.connection.execute(<<~SQL)
DELETE FROM attacks;
DELETE FROM monsters;
DELETE FROM victims;
ALTER SEQUENCE monsters_id_seq RESTART WITH 1;
ALTER SEQUENCE victims_id_seq RESTART WITH 1;
ALTER SEQUENCE attacks_id_seq RESTART WITH 1;
INSERT INTO
monsters (scare_level, name, description, created_at, updated_at)
VALUES
(8, 'Drácula', 'Chupa sange. Hincha del rojo. Libertario. ALA', now(), now()),
(6, 'King Kong', 'Gorila gigante. #VamosAVolver. BocaJrs.', now(), now()),
(4, 'Nahuelito', 'Vivo en el Nahuel Huapi. Soltero. Fanático del plancton.', now(), now()),
(5, 'Hombre lobo', 'Mitad lobo - mitad humano', now(), now()),
(1, 'James P. Sullivan', '#monstropolis #scareroftheyear', now(), now());
INSERT INTO
victims (name, birthdate, created_at, updated_at)
VALUES
('Flor', '2001-02-27', now(), now()),
('Hugo', '1944-01-09', now(), now()),
('Lola', '1999-03-10', now(), now()),
('Paco', '1974-10-25', now(), now()),
('Pepe', '2009-07-12', now(), now()),
('Alma', '2008-03-23', now(), now());
INSERT INTO
attacks (monster_id, victim_id, created_at, updated_at)
VALUES
(1, 1, now(), now()),
(3, 2, now(), now()),
(3, 4, now(), now()),
(3, 5, now(), now()),
(5, 6, now(), now());
SQL

puts "Done."

0 comments on commit c78bbeb

Please sign in to comment.