FWIW I've found a workaround for this: Instead of binding the :collection attribute of the select element to a DM collection, I populate an array first, then bind to the array.
So instead of this: <%= select :supplier_id, :label => 'Supplier', :collection => Supplier.all(:order => [:name]), :value_method => :id, :text_method => :name_and_currency %> I'm using this: <% @suppliers.each do |supplier| supplier_list << [ supplier.id, supplier.display_name ] end %> <%= select :supplier_id, :label => 'Supplier', :collection => supplier_list %> Using my own 'each' loop successfully encourages DM to do it's Stategic Eager Loading magic. I can see why this is the case, though it raises the question of whether the Merb/Rails3 select list could be refactored to encourage SEL?! Hope this helps someone. George On Dec 29, 12:52 pm, George <[email protected]> wrote: > Hi there, > > I notice from my dm log that when I populate a picklist from a model > it is fetching an associated field by running a query for EVERY item > in the list. I know this is because I am also displaying the > associated field value in the list but how can I encourage dm to eager > load the associated fields in the initial query? > > Here's the model and the association: > > class Supplier > property :id, Serial > property :name, String, :default => 'New supplier' > belongs_to :currency, :model => "ExchangeRate", :child_key => > [:currency_id] > > # Supplier name and currency string: (Eg: "British Airways [GBP]") > def name_and_currency > name = self.name.empty? ? '(blank supplier name)' : self.name > currency = self.currency && self.currency.name || '(no currency)' > return name + " [" + currency + "]" > end > end > > Here's the attempt to populate the list in merb view: (Note use of > custom name_and_currency method) > <%= select :supplier_id, :label => 'Supplier', :collection => > Supplier.all(:order => [:name]), :value_method => :id, :text_method > => :name_and_currency %> > > Many thanks, > George > > PS: I think this old post was trying to achieve the same thing but > perhaps the syntax has changed since > then:http://groups.google.com/group/datamapper/browse_thread/thread/676d43... -- You received this message because you are subscribed to the Google Groups "DataMapper" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/datamapper?hl=en.
