Skip to content

Instantly share code, notes, and snippets.

@protocarl
Created June 24, 2009 22:12
Show Gist options
  • Save protocarl/135550 to your computer and use it in GitHub Desktop.
Save protocarl/135550 to your computer and use it in GitHub Desktop.
Typecasting / SEL issue
require 'rubygems'
gem 'dm-core', '0.10.0'
require 'dm-core'
class Post
include DataMapper::Resource
def self.default_repository_name
:post
end
property :id, Serial
property :title, String
has n, :comments, :repository => :comment
end
class Comment
include DataMapper::Resource
def self.default_repository_name
:comment
end
property :id, Serial
property :body, Text
property :post_id, String # for argument sake
belongs_to :post, :repository => :post
end
DataMapper.setup(:comment, "sqlite3::memory:")
DataMapper.setup(:post, :adapter => :in_memory)
DataObjects::Sqlite3.logger = DataObjects::Logger.new(STDOUT, 0)
DataMapper.auto_migrate!
Post.create(:title => 'Post 1')
Post.create(:title => 'Post 2')
# !> method redefined; discarding old to_datetime
Comment.create(:body => 'first!', :post_id => '1')
Comment.create(:body => 'first!', :post_id => '2')
Comment.all.map { |comment| comment.post } # => [nil, nil]
# >> Thu, 25 Jun 2009 17:53:16 GMT ~ debug ~ (0.000511) SELECT sqlite_version(*)
# >> Thu, 25 Jun 2009 17:53:16 GMT ~ debug ~ (0.000399) DROP TABLE IF EXISTS "comments"
# >> Thu, 25 Jun 2009 17:53:16 GMT ~ debug ~ (0.000042) PRAGMA table_info("comments")
# >> Thu, 25 Jun 2009 17:53:16 GMT ~ debug ~ (0.001048) CREATE TABLE "comments" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "body" TEXT, "post_id" VARCHAR(50))
# >> Thu, 25 Jun 2009 17:53:16 GMT ~ debug ~ (0.000115) INSERT INTO "comments" ("body", "post_id") VALUES ('first!', '1')
# >> Thu, 25 Jun 2009 17:53:16 GMT ~ debug ~ (0.000057) INSERT INTO "comments" ("body", "post_id") VALUES ('first!', '2')
# >> Thu, 25 Jun 2009 17:53:16 GMT ~ debug ~ (0.000081) SELECT "id", "post_id" FROM "comments" ORDER BY "id"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment