Created
July 4, 2011 20:49
-
-
Save ogredude/1063926 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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