Created
October 11, 2013 22:05
-
-
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!
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
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