Skip to content

Instantly share code, notes, and snippets.

@innovationhero
Forked from remigijusj/gist:870925
Created September 5, 2012 20:06
Show Gist options
  • Save innovationhero/3643736 to your computer and use it in GitHub Desktop.
Save innovationhero/3643736 to your computer and use it in GitHub Desktop.
dm-redis-adapter with "through" associations
require 'dm-core'
require 'dm-redis-adapter'
DataMapper.setup(:default, {:adapter => "redis"})
class Book
include DataMapper::Resource
property :id, Serial
property :name, String
has n, :book_tags
has n, :tags, :through => :book_tags
end
class Tag
include DataMapper::Resource
property :id, Serial
property :name, String
has n, :book_tags
has n, :books, :through => :book_tags
end
class BookTag
include DataMapper::Resource
property :id, Serial
# property :book_id, Integer, :index => true
# property :tag_id, Integer, :index => true
belongs_to :book, :key => true
belongs_to :tag, :key => true
end
Redis.new.flushdb
b = Book.create(:name => "Harry Potter")
t = Tag.create(:name => "fiction")
b.tags << t
b.save
b2 = Book.get(1)
p b2 # #<Book @id=1 @name="Harry Potter">
p b # #<Book @id=1 @name="Harry Potter">
puts(b2 == b) # true
p b.tags # [#<Tag @id=1 @name="fiction">]
p b2.tags # [] # !!!
p b2.book_tags # [] # !!!
# after uncommenting BookTag property declarations
# b2.book_tags is correct, but b2.tags still empty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment