On Jan 9, 4:38 pm, Matt Tuzzolo <[email protected]> wrote: > I'm porting some code from PHP to Ruby and have decided on using > Sequel. I have a query that uses some math functions to determine a > distance... > > $query = 'SELECT *, ( 3963 * ACOS( COS( RADIANS(' .$lat. ') ) * COS > ( RADIANS( lat ) ) * COS( RADIANS(lng) - RADIANS(' .$lng. ') ) + SIN > ( RADIANS(' .$lat. ') ) * SIN( RADIANS( lat ) ) ) ) AS distance FROM > locations HAVING distance < 5000 ORDER BY distance ASC'; > > I'm trying to figure out how I can get this to work with Sequel. As > far as I can tell there isn't a way to use raw SQL with dataset.filter > (). I'm trying to avoid using ActiveRecord as I want to leave myself > with the option of running this code outside of Rails. > > Can anyone point me in the right direction here? Surely this is > possible to do w/ Sequel. > > Regards, > > -Matt Tuzzolo
I wrote a simple sequel plugin to do distance searches with sequel. Just created a gist for this at: http://gist.github.com/273784 Let me know if you find it useful. Here is an example of using it in a Home class. class Home < Sequel::Model # 50 miles DEFAULT_BOUNDARY = 50 plugin :geosearch ## # Filter dataset to homes that have been geocoded def_dataset_method(:geocoded) do filter({~:lat => nil},{~:lng => nil}) end ## # Filter dataset ordered by how close a home is from another. def_dataset_method(:near) do |geo| geocoded.distance_from(geo).order(:distance) end ## # Filter dataset to other homes that are within a certain distance # or default boundary from a location. def_dataset_method(:around) do |*args| geo = args[0] limit = args[1] || DEFAULT_BOUNDARY geocoded.distance_boundary(geo,limit) end ## # Homes near this instance def near self.geocoded? ? self.class.near(self) : [] end end
-- You received this message because you are subscribed to the Google Groups "sequel-talk" 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/sequel-talk?hl=en.
