Created
March 5, 2011 15:48
-
-
Save solnic/856457 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
require 'dm-core' | |
require 'dm-types' | |
require 'dm-migrations' | |
require 'dm-validations' | |
DataMapper::Logger.new($stdout, :debug) | |
DataMapper.setup :default, "sqlite::memory::" | |
class Person | |
include DataMapper::Resource | |
property :id, Serial | |
end | |
class Loan | |
include DataMapper::Resource | |
property :id, UUID, :key => true, :default => lambda { |r, p| UUIDTools::UUID.random_create } | |
end | |
class Ticket | |
include DataMapper::Resource | |
property :id, Serial | |
property :loan_id, UUID, :unique => :loaner | |
belongs_to :loaner, "Person" | |
belongs_to :loanee, "Person" | |
belongs_to :loan | |
validates_with_block { loaner != loanee } | |
end | |
DataMapper.finalize.auto_migrate! | |
loaner = Person.create | |
loanee = Person.create | |
loan = Loan.create | |
t1 = Ticket.create(:loaner => loaner, :loanee => loanee, :loan => loan) | |
t2 = Ticket.new(:loaner => loaner, :loanee => loanee, :loan => loan) | |
puts t2.valid? | |
puts t2.errors.inspect |
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
~ (0.000069) SELECT sqlite_version(*) | |
~ (0.170509) DROP TABLE IF EXISTS "people" | |
~ (0.000036) PRAGMA table_info("people") | |
~ (0.157415) CREATE TABLE "people" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT) | |
~ (0.157829) DROP TABLE IF EXISTS "loans" | |
~ (0.000031) PRAGMA table_info("loans") | |
~ (0.174376) CREATE TABLE "loans" ("id" VARCHAR(36) NOT NULL, PRIMARY KEY("id")) | |
~ (0.182974) DROP TABLE IF EXISTS "tickets" | |
~ (0.000032) PRAGMA table_info("tickets") | |
~ (0.174169) CREATE TABLE "tickets" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "loan_id" VARCHAR(36), "loaner_id" INTEGER NOT NULL, "loanee_id" INTEGER NOT NULL) | |
~ (0.166520) CREATE INDEX "index_tickets_loaner" ON "tickets" ("loaner_id") | |
~ (0.166484) CREATE INDEX "index_tickets_loanee" ON "tickets" ("loanee_id") | |
~ (0.208128) CREATE UNIQUE INDEX "unique_tickets_loaner" ON "tickets" ("loan_id") | |
~ (0.315982) INSERT INTO "people" DEFAULT VALUES | |
~ (0.249397) INSERT INTO "people" DEFAULT VALUES | |
~ (0.140535) INSERT INTO "loans" ("id") VALUES ('724deb54-72de-4cb9-a8eb-ff23d93a39d4') | |
~ (0.000077) SELECT "id" FROM "tickets" WHERE ("loan_id" = '724deb54-72de-4cb9-a8eb-ff23d93a39d4' AND "loaner_id" = 1) ORDER BY "id" LIMIT 1 | |
~ (0.146969) INSERT INTO "tickets" ("loan_id", "loaner_id", "loanee_id") VALUES ('724deb54-72de-4cb9-a8eb-ff23d93a39d4', 1, 2) | |
~ (0.000077) SELECT "id" FROM "tickets" WHERE ("loan_id" = '724deb54-72de-4cb9-a8eb-ff23d93a39d4' AND "loaner_id" = 1) ORDER BY "id" LIMIT 1 | |
false | |
#<DataMapper::Validations::ValidationErrors:0x95d86e0 @resource=#<Ticket @id=nil @loan_id=#<UUID:0x4afb456 UUID:724deb54-72de-4cb9-a8eb-ff23d93a39d4> @loaner_id=1 @loanee_id=2>, @errors={:loan_id=>["Loan is already taken"]}> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment