forked from I110IS/lab2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
335dfed
commit c78bbeb
Showing
6 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." |