Last active
February 8, 2016 21:09
-
-
Save lsimoneau/4591743 to your computer and use it in GitHub Desktop.
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 'test/unit/assertions' | |
require 'dm-core' | |
require 'dm-sqlite-adapter' | |
require 'dm-migrations' | |
DataMapper.setup :default, "sqlite::memory:" | |
class Tag | |
include DataMapper::Resource | |
property :id, Serial | |
property :category, String | |
has n, :listing_tags | |
has n, :tags, :through => :listing_tags | |
end | |
class ListingTag | |
include DataMapper::Resource | |
property :id, Serial | |
belongs_to :listing | |
belongs_to :tag | |
end | |
class Listing | |
include DataMapper::Resource | |
property :id, Serial | |
has n, :listing_tags | |
has n, :tags, :through => :listing_tags | |
end | |
DataMapper.auto_migrate! | |
listing = Listing.create | |
listing.tags.create(category: "niche") | |
listing.listing_tags.reload | |
include Test::Unit::Assertions | |
# this passes, the id of the tag matches the tag_id of the listing_tag | |
assert_equal listing.tags.last.id, listing.listing_tags.last.tag_id | |
listing.listing_tags.tags(category: "other") | |
# this fails, the tag_id of the listing_tag is now nil | |
assert_equal listing.tags.last.id, listing.listing_tags.last.tag_id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm curious, should https://gist.github.com/lsimoneau/4591743#file-dm_association_bug-rb-L15 be
has n :listings …
?