samedi 12 septembre 2015

Rails rolls back valid object

I've got (what I thought was) a very standard object to be saved in Rails 4:

create_table "games", force: true do |t|
    t.string   "sport",      default: "",    null: false
    t.string   "opponent",   default: "",    null: false
    t.datetime "date"
    t.integer  "home_score", default: 0,     null: false
    t.integer  "op_score",   default: 0,     null: false
    t.boolean  "active",     default: true,  null: false
    t.boolean  "final",      default: false, null: false
    t.datetime "created_at"
    t.datetime "updated_at"
end

The object itself:

sport: "Foosball", opponent: "New York", 
date: "2013-12-31 00:00:00", 
home_score: 100, op_score: 0, active: false, final: true,

game.rb:

class Game < ActiveRecord::Base
    validates_presence_of :opponent, :sport
    before_save :check_active
    scope :active, -> { where(active:true) }

    def check_active
      if self.date <= (Date.today - 4)
         self.active = false;
      end
    end
end

However, when I go to save the object:

pry(#<GamesController>)> @game.save
(0.1ms)  begin transaction
(0.1ms)  rollback transaction
    => false
[14] pry(#<GamesController>)> @game.errors.messages
    => {}

What is going on? I've noticed the server also returns 422 unproccessable entity, but I'm not sure how that's happening. The object seems fine in all respects.

Aucun commentaire:

Enregistrer un commentaire