Q:

ActiveRecord validation not working

I've created Rails ActiveRecord validation, and when it is violated, no record is being saved (Actually, I believe it is being saved and then a ROLLBACK is being done as seen in console output). 
But, instead of being redirected to the new form again and displaying the error message I added, I'm sent to the index page (with no new record created, which is right, but i would like the other behaviour). 
I believe is a silly thing, related to the rollback (like the record is being save, that's why i'm redirected to the index page, but then the rollback is done - Don't know why, and no record is saved). Any idea how to fix this?
I've created the following validation:
  # Adds an error if the replacement date is in the future
  def replacement_date_is_not_future
    unless replacement_date.blank? or replacement_date <= Date.today
      errors.add(:replacement_date, :date)
    end
  end

and at the head of my model i have the following:
  validate :replacement_date_is_not_future

this is my controller method:
  def create
    @device_replacement = DeviceReplacementForm.new(replacement_params)

    if @device_replacement.save
      redirect_to device_replacements_path
    else
      render 'new'
    end
  end

this is my console output:
Started POST "/device_replacements" for 127.0.0.1 at 2015-12-31 10:48:09 -0300
Processing by DeviceReplacementsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"MbR2lXp5ujSNILEKC2ROhij3+yNpIL3ymEdZxJejSxc=", "device_replacement_form"=>{"old_esn"=>"2-3021325", "new_esn"=>"DEMO-0-648340", "replacement_date"=>"2016-01-03"}, "commit"=>"Create"}
  RackCAS::ActiveRecordStore::Session Load (0.9ms)  SELECT  "sessions".* FROM "sessions"  WHERE "sessions"."session_id" = '0ee02a1bcbc8548e172d511c02b97490'  ORDER BY "sessions"."id" ASC LIMIT 1
  Device Load (1.1ms)  SELECT  "devices".* FROM "devices"  WHERE "devices"."esn" = '2-3021325' LIMIT 1
  Device Load (1.6ms)  SELECT  "devices".* FROM "devices"  WHERE "devices"."esn" = 'DEMO-0-648340' LIMIT 1
   (0.3ms)  BEGIN
   (0.5ms)  SELECT "devices"."asset_id" FROM "devices"  WHERE "devices"."id" = 81265
   (0.3ms)  ROLLBACK
Redirected to http://localhost:3000/device_replacements
Completed 302 Found in 93ms (ActiveRecord: 14.8ms)
  RackCAS::ActiveRecordStore::Session Load (1.9ms)  SELECT  "sessions".* FROM "sessions"  WHERE "sessions"."session_id" = '0ee02a1bcbc8548e172d511c02b97490' LIMIT 1
   (0.3ms)  BEGIN
   (0.4ms)  COMMIT

A:

seems like your code is fine, the thing is that is not in the right place. try to look if there's another model in charge of creating the device_replacement in that view. 
Hope it helps.

