2.5 - Scope alive

pull/1/head
Karim Bogtob 2 years ago
parent 180c1735d0
commit f5bdb92a2a

@ -40,3 +40,29 @@ Question 3.2 :
* `rails db:seed` * `rails db:seed`
## TP4
Question 2.5 :
* J'ajoute une créature avec des PVs à 0 :
```ruby
rails console
Loading development environment (Rails 7.0.4.3)
irb(main):001:0> Creature.create(name: 'Zombie', health_points: 0)
TRANSACTION (0.1ms) begin transaction
Creature Create (0.4ms) INSERT INTO "creatures" ("name", "health_points", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "Zombie"], ["health_points", 0], ["created_at", "2023-03-26 14:00:56.413028"], ["updated_at", "2023-03-26 14:00:56.413028"]]
TRANSACTION (0.9ms) commit transaction
=>
#<Creature:0x00000001104b9310
id: 27,
name: "Zombie",
health_points: 0,
created_at: Sun, 26 Mar 2023 14:00:56.413028000 UTC +00:00,
updated_at: Sun, 26 Mar 2023 14:00:56.413028000 UTC +00:00>
```
* J'essaie de renommer la créature dans postman et je vérifie que son nom ne change pas
* J'ai bien une 404 et son nom n'a pas changé

@ -21,7 +21,7 @@ class CreaturesController < ApplicationController
end end
def update def update
@creature = Creature.find(params[:id]) @creature = Creature.alive.find(params[:id])
@creature.update!(create_or_update_params) @creature.update!(create_or_update_params)
render json: @creature.as_json(only: SHOWABLE_ATTRIBUTES) render json: @creature.as_json(only: SHOWABLE_ATTRIBUTES)
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound

@ -1,4 +1,6 @@
class Creature < ApplicationRecord class Creature < ApplicationRecord
scope :alive, -> { where('health_points > 0') }
def to_label def to_label
"#{name} (#{health_points})" "#{name} (#{health_points})"
end end

Loading…
Cancel
Save