Created
October 21, 2010 14:16
-
-
Save solnic/638561 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
| require "dm-core" | |
| require "dm-migrations" | |
| require "dm-validations" | |
| require "dm-validations-ext" | |
| DataMapper.setup :default, "sqlite::memory" | |
| class User | |
| include DataMapper::Resource | |
| property :id, Serial | |
| property :name, String | |
| belongs_to :group | |
| has n, :roles | |
| end | |
| class Group | |
| include DataMapper::Resource | |
| property :id, Serial | |
| property :name, String, :length => 10..255 | |
| has n, :users | |
| end | |
| class Role | |
| include DataMapper::Resource | |
| property :id, Serial | |
| property :name, String, :length => 4..10 | |
| belongs_to :user | |
| end | |
| DataMapper.finalize | |
| DataMapper.auto_migrate! | |
| user = User.new(:name => "John") | |
| group = Group.new(:name => "Too Short") | |
| role = Role.new(:name => "Way Too Long") | |
| user.group = group | |
| user.roles << role | |
| puts user.save | |
| puts "*"*80 | |
| puts user.errors[:group].inspect | |
| puts "*"*80 | |
| puts user.errors[:roles].inspect |
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
| false | |
| ******************************************************************************** | |
| #<DataMapper::Validations::ValidationErrors:0xa098a30 @resource=#<Group @id=nil @name="Too Short">, @errors={:name=>["Name must be between 10 and 255 characters long"]}> | |
| ******************************************************************************** | |
| [#<DataMapper::Validations::ValidationErrors:0xa0931ac @resource=#<Role @id=nil @name="Way Too Long" @user_id=nil>, @errors={:name=>["Name must be between 4 and 10 characters long"], :user_id=>["User must not be blank"]}>] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment