Skip to content

Instantly share code, notes, and snippets.

@solnic
Created October 21, 2010 14:16
Show Gist options
  • Select an option

  • Save solnic/638561 to your computer and use it in GitHub Desktop.

Select an option

Save solnic/638561 to your computer and use it in GitHub Desktop.
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
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