I am dealing with rails 3..5, and that i designed a custom validation for my model, after i create or update a container record, the validation is effectively performed and add errors if this needs to, however the record continues to be held in database.

I have examined all the custom validation method and works fine.

Here's my model and custom validation:

class Container < ActiveRecord::Base
  include Comparable
  belongs_to :booking

  validates :booking, presence: true
  validates :kind, presence: true
  validates :dimension, presence: true, numericality: { only_integer: true }
  validates :retirement_location, presence: true
  validates :amount, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 1 }
  validate :uniqueness_of_single_fields

  scope :group, lambda {|k,d| where('kind = ? and dimension = ?',k,d) }

  def self.to_hash
    {
      "kind" => self.kind.to_s,
      "dimension" => self.dimension.to_s,
      "retirement_location" => self.retirement_location.to_s
    }
  end

  def uniqueness_of_single_fields
    similar_containers = Container.joins(:booking).where('bookings.requirement_id = ? and containers.id != ?', booking.requirement.id, id)
    similar_containers = similar_containers.where(assigned_unit: assigned_unit, assigned_seal: assigned_seal)

    unless similar_containers.empty?
      errors.add(:base, "esa unidad y sello ya han sido asignados")
    end
  end

  def <=>(other)
    result = kind <=> other.kind
    result == 0 ? dimension <=> other.dimension : result
  end
end

P.S. I have checked previous questions, but none of them of then possess the response to this

I designed a rspec test to do this

require 'spec_helper'

describe Container do
  it 'should have a uniq combination assigned unit and seal for in a requirement' do
    requirement1 = Factory(:requirement, reference: 'abc123')
    requirement2 = Factory(:requirement, reference: 'qwerty')

        booking1 = Factory(:booking, reference: 'FOOBAR123', requirement: requirement1)
    booking2 = Factory(:booking, reference: 'FOOBAR456', requirement: requirement1)
    booking3 = Factory(:booking, reference: 'FOOBAR789', requirement: requirement2)

    container1 = Factory(:container, assigned_unit: 'foo1', assigned_seal: 'bar1', booking: booking1)
    container2 = Factory.build(:container, assigned_unit: 'foo1', assigned_seal: 'bar1', booking: booking2)
    container3 = Factory(:container, assigned_unit: 'foo1', assigned_seal: 'bar1', booking: booking3)

    container1.should be_valid
    container3.should be_valid

    puts container2.errors
    container2.save
    puts container2.inspect
    container2.should_not be_valid
    puts container2.errors.inspect
    container2.errors[:base].first.should == "esa unidad y sello ya han sido asignados"

    c = Requirement.new(client: 'foo')
    c.save
    puts c.errors
    puts c.inspect
  end
end

and also the output is: (It passes if this should fail)

{}
#<Container id: 6, kind: "dry", dimension: 20, retirement_location: "sitrans", booking_id: 5, created_at: "2011-03-28 22:31:13", updated_at: "2011-03-28 22:31:13", load: "wine 2", assigned_unit: "foo1", assigned_seal: "bar1", temp_celsius: nil, temp_fahrenheit: nil, vent_percentage: nil, vent_cfm: nil, generator: nil, amount: 1>
{:base=>["esa unidad y sello ya han sido asignados"]}
{:service=>["Debe elegir un tipo de servicio"], :shipping_company=>["Debe haber una naviera relacionada con esta orden de servicio"]}
#<Requirement id: nil, client: "foo", service_id: nil, realization: nil, hour: nil, shipping_company_id: nil, description: nil, created_at: nil, updated_at: nil, reference: nil, state: "new">
.

Finished in 0.45293 seconds
1 example, 0 failures