Hello everyone, I’m taking Rails for Zombies 2 course. I’m doing everything as in a video, but I still have a mistake. I can not create a new zombie.
When I enter:

z = Zombie.create 

This error appears:

  (0.1ms) begin transaction (0.1ms) rollback transaction NoMethodError: undefined method `>' for nil:NilClass from /home/basania/RubymineProjects/Tweeter/app/models/zombie.rb:9:in `make_rotting' from /home/basania/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:432:in `block in make_lambda' from /home/basania/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:164:in `call' from 

and so on, the log is quite large. Here is the code in the Zombie model:

 class Zombie < ActiveRecord::Base scope :rotting, -> { where(rotting: true) } scope :fresh, -> { where('age < 20') } scope :recent, -> { order('created_at desc').limits(3) } has_one :brain before_save :make_rotting def make_rotting self.rotting = true if age > 20 end end 

ZombiesController controller ZombiesController :

 def update @zombie = Zombie.find(params[:id]) #Эту строка добавлялась respond_to do |format| #... // ... // создавалось скафолдом, там все методы CRUD end 

and RottingController controller:

 class RottingZombiesController < ApplicationController def index @rotting_zombies = Zombie.rotting end end 

I apologize for the cumbersome, if you need some more code, I'll throw it here. If possible, help on Skype: carlson444.

  • It looks like age empty. - Nakilon
  • @Nakilon where to register it? - basania
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

It seems that your migration does not set the default value for the field: age. Consequently,

 before_save make_rotting 

trying to handle a condition:

 nil > 20 

There are several ways out of this situation:

1) Hang the condition on make_rotting :

 before_save make_rotting, if: :age? 

2) Add migration:

 class SetDefaultAgeToZombies < ActiveRecord::Migration def change change_column :zombies, :age, :integer, default: 0 end end 

PS And check the code for the course. It is unlikely that the authors could leave such an error. Or, the course code does not involve creating a record without specifying age.