Skip to content

Instantly share code, notes, and snippets.

@ogredude
Created July 4, 2011 20:49
Show Gist options
  • Save ogredude/1063926 to your computer and use it in GitHub Desktop.
Save ogredude/1063926 to your computer and use it in GitHub Desktop.
class Order < ActiveRecord::Base
belongs_to :person
has_and_belongs_to_many :items
validate :requires_at_least_one_item
def requires_at_least_one_item
errors.add(:items, "requires at least one Item") if items.empty?
end
end
require 'spec_helper'
describe Order do
it {should belong_to(:person)}
it {should have_and_belong_to_many(:items)}
it "should have at least 1 item" do
o = Factory.build(:order, :items => [])
o.should_not be_valid
o.errors[:items].should_not be_empty
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment