-
-
Save innovationhero/3643736 to your computer and use it in GitHub Desktop.
dm-redis-adapter with "through" associations
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-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