Brian Sheehan
2007-Aug-28 12:24 UTC
[Betternestedset-talk] eager loading of associations by full_set
Hi, Has anyone considered adding functionality to the #full_set method that would allow it to eager load associations? An example implementation would be this: # Returns itself and all nested children. # Pass :exclude => item, or id, or [items or id] to exclude one or more items *and* all of their descendants. def full_set(special={}) if special[:exclude] exclude_str = " AND NOT (#{base_set_class.sql_for(special[:exclude])}) " elsif new_record? || self[right_col_name] - self[left_col_name] == 1 return [self] end base_set_class.find(:all, :conditions => "#{scope_condition} #{exclude_str} AND (#{left_col_name} BETWEEN #{self[left_col_name]} AND #{self[right_col_name]})", :order => left_col_name, :include => special[:include]) end The the main difference in the above code is that it adds a :include parameter to the finder call at the end of the method. It would be used like this: class Location < ActiveRecord::Base acts_as_nested_set has_many :buildings end all_locations = Location.root.full_set :include => :buildings all_buildings = all_locations.collect {|l| l.buildings} The point of it would be that you could, using the above example, get a list of all buildings in a location with only one db access. Anyone have any thoughts about something like this? Brian