Skip to content

Instantly share code, notes, and snippets.

@pnomolos
Created January 9, 2012 19:09
Show Gist options
  • Save pnomolos/1584408 to your computer and use it in GitHub Desktop.
Save pnomolos/1584408 to your computer and use it in GitHub Desktop.
Non-working has 1, :through => :model relationship
require 'data_mapper'
DataMapper::Logger.new($stdout)
DataMapper.setup(:default, 'sqlite::memory:')
class Pack
include DataMapper::Resource
belongs_to :school
has n, :pack_orders
property :id, Serial
end
class PackOrder
include DataMapper::Resource
belongs_to :pack
has 1, :school, :through => :pack
# I tried :belongs_to but DM throws an
# error about using :through with belongs_to
property :id, Serial
end
class School
include DataMapper::Resource
has n, :packs
has n, :pack_orders, :through => :packs
property :id, Serial
end
DataMapper.finalize
DataMapper.auto_migrate!
@school = School.create
@pack = Pack.create :school => @school
@pack_order = PackOrder.create :pack => @pack
# Works
@orders = PackOrder.all :pack => { :school => @school }
@orders.each do |order|
puts order.id
end
# Doesn't work
@orders = PackOrder.all :school => @school
@orders.each do |order|
puts order.id
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment