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/676d4353a796f68a/e819d6ba2aa80128?lnk=gst&q=eager+load#e819d6ba2aa80128

--

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.


Reply via email to