Created
January 27, 2010 19:27
-
-
Save slaskis/288098 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 "rubygems" | |
require "dm-core" | |
require "dm-timestamps" | |
require "pp" | |
class Entry | |
include DataMapper::Resource | |
has n, :related, :through => Resource, :model => Entry | |
property :id, Serial | |
property :title, String, :length => 255 | |
property :class_name, Discriminator | |
property :created_at, DateTime | |
property :updated_at, DateTime | |
property :published_at, DateTime | |
def get_related(query = nil) | |
links = related.query.links.delete_if {|r| related.query.model.storage_name(repository_name) == r.source_model.storage_name(repository_name) } | |
related( (query||{}).merge( :links => links ) ) | |
end | |
end | |
class Person < Entry | |
property :name, String, :length => 255 | |
property :email, String, :length => 255 | |
end | |
class Work < Entry | |
property :body, Text | |
end | |
DataMapper::Logger.new(STDOUT,:debug) | |
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/bula.db") | |
DataMapper.auto_migrate! | |
a = Work.create( :title => "ABC" , :body => "Bla bla..." ) | |
b = Work.create( :title => "DEF" , :body => "Bla bla..." ) | |
a.related << b | |
a.save | |
p a.get_related |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment