Skip to content

Instantly share code, notes, and snippets.

@igor-alexandrov
Created October 11, 2013 22:05
Show Gist options
  • Save igor-alexandrov/6942754 to your computer and use it in GitHub Desktop.
Save igor-alexandrov/6942754 to your computer and use it in GitHub Desktop.
Complete example of how to map association from Sequel::Model to AR. Eager loading works like a charm!
class Tour < Sequel::Model
many_to_one :vendor_pansion,
:dataset => proc{ Vendor::Pansion.where(:id => self.vendor_pansion_id) },
:reciprocal => nil, :class => 'Vendor::Pansion',
:eager_loader => (proc do |opts|
opts[:rows].each{ |object| object.associations[:vendor_pansion] = nil }
Vendor::Pansion.where(:id => opts[:id_map].keys).each do |vendor_pansion|
if tours = opts[:id_map][vendor_pansion.id]
tours.each do |tour|
tour.associations[:vendor_pansion] = vendor_pansion
end
end
end
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment