Skip to content

Instantly share code, notes, and snippets.

@ymendel
Created July 28, 2008 02:58
Show Gist options
  • Save ymendel/2831 to your computer and use it in GitHub Desktop.
Save ymendel/2831 to your computer and use it in GitHub Desktop.
class Category < ActiveRecord::Base
has_many :items
validates_presence_of :name
validates_uniqueness_of :name
end
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
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