Skip to content

Instantly share code, notes, and snippets.

@lsimoneau
Last active February 8, 2016 21:09
Show Gist options
  • Save lsimoneau/4591743 to your computer and use it in GitHub Desktop.
Save lsimoneau/4591743 to your computer and use it in GitHub Desktop.
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
@tpitale
Copy link

tpitale commented Feb 8, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment