Created
July 28, 2008 02:58
-
-
Save ymendel/2831 to your computer and use it in GitHub Desktop.
This file contains 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 Category < ActiveRecord::Base | |
has_many :items | |
validates_presence_of :name | |
validates_uniqueness_of :name | |
end |
This file contains 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
Cassady:~/dev/scratch/plugin-testing yossef$ script/console | |
Loading development environment (Rails 2.0.2) | |
/>> cat = Category.new | |
=> #<Category id: nil, name: nil> | |
>> cat.name = 'testing' | |
=> "testing" | |
>> i = Item.new | |
=> #<Item id: nil, code: nil, category_id: nil> | |
>> i.code = 'test-1234' | |
=> "test-1234" | |
>> i.valid? | |
=> false | |
>> i.errors.full_messages | |
=> ["Category can't be blank"] | |
>> cat.items << i | |
=> [#<Item id: nil, code: "test-1234", category_id: nil>] | |
>> i.valid? | |
=> false | |
>> i.errors.full_messages | |
=> ["Category can't be blank"] | |
>> cat.save | |
=> false | |
>> cat.errors | |
=> #<ActiveRecord::Errors:0x23d06c4 @base=#<Category id: nil, name: "testing">, @errors={"items"=>["is invalid"]}> | |
>> exit |
This file contains 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 Item < ActiveRecord::Base | |
belongs_to :category | |
validates_presence_of :category | |
validates_presence_of :code | |
validates_uniqueness_of :code | |
validates_format_of :code, :with => /^[a-zA-Z]+-\d+$/ | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment