Skip to content

Instantly share code, notes, and snippets.

@pedrodelgallego
Forked from beccasaurus/gist:294064
Created February 4, 2010 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pedrodelgallego/294275 to your computer and use it in GitHub Desktop.
Save pedrodelgallego/294275 to your computer and use it in GitHub Desktop.
require "rubygems"
require "dm-core"
class Word
include DataMapper::Resource
property :id, Serial
property :text, String
has n, :translations #, :model => 'Word', :foreign_key => [:word_id]
has n, :words, :through => :translations, :foreign_key => [:word_id]
end
class Translation
include DataMapper::Resource
property :word_id, Integer, :key => true
property :translation_id, Integer, :key => true
belongs_to :word, :model => 'Word', :child_key => [:word_id]
belongs_to :translation, :model => 'Word', :child_key => [:translation_id]
end
DataMapper.setup(:default, 'sqlite3::memory:')
Word.auto_migrate!
Translation.auto_migrate!
t = Translation.create :word => Word.create(:text => 'y'), :translation => Word.create(:text => 'x')
puts t.inspect
puts Word.first.translations.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment