Created
February 23, 2011 18:15
-
-
Save cdemyanovich/840862 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 | |
has_many :line_items, :dependent => :destroy, :order => "position" | |
accepts_nested_attributes_for :line_items, :allow_destroy => true, :reject_if => lambda { |attributes| attributes["amount"].blank? } | |
validates_associated :line_items | |
private | |
def validate | |
self.errors.add_to_base("An order needs at least one line item") if self.line_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
describe Order, "being valid" do | |
it "requires at least one line item" do | |
order = Order.new | |
order.should_not be_valid | |
order.errors.on_base.should include("An order needs at least one line item") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment