sql - Datamapper "first" method is querying the whole relation -


i'm using datamapper 1.0 sinatra , sqlite3 backend.

i have devices, have multiple locations, , want latest location of device. problem generated sql above line query locations device not latest 1 (and device can have 10-20k locations). not optimal, i'm asking: doing wrong? or datamapper works way , have resort plain sql? there better workaround?

i have following data schema:

class location   include datamapper::resource   property :id, serial   property :lat, integer   property :lon, integer   property :time, time   belongs_to :device end  class device   include datamapper::resource   property :id, serial   has n, :locations    def lat # , lon     locations.first(:order => [:time.desc]).lat   end end  

here call d.first.lat (where d device acquired using d = devices.all) result in query of locations, instead of first matching one.

i have these 2 models, , querying simple too:

  • in controller: @devices = device.all
  • in view: @devices.each { |d| d.lat }
  • and in model: def lat; locations.first.lat; end

thanks.

i think trick use has 1 assocation

class device   include datamapper::resource   property :id, serial   has n, :locations # want :order => [:time.desc] here   has 1, :latest_location, :class_name => location, :order => [:time.desc]    def lat     latest_location.lat   end end 

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -