Skip to content

Commit

Permalink
Use separate :offset and :limit query options.
Browse files Browse the repository at this point in the history
PostgreSQL doesn't support the "X,Y" format for the LIMIT clause, and this
is proper use of ActiveRecord anyway.

In response to bug reported by Github user kenzie.
  • Loading branch information
alexreisner committed Jun 3, 2010
1 parent fb13aa6 commit dfb6d81
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions lib/geocoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module ClassMethods
#
# +order+ :: column(s) for ORDER BY SQL clause
# +limit+ :: number of records to return (for LIMIT SQL clause)
# +offset+ :: number of records to skip (for LIMIT SQL clause)
# +offset+ :: number of records to skip (for OFFSET SQL clause)
#
def near_scope_options(latitude, longitude, radius = 20, options = {})
if ActiveRecord::Base.connection.adapter_name == "SQLite"
Expand Down Expand Up @@ -88,7 +88,8 @@ def full_near_scope_options(latitude, longitude, radius, options)
coordinate_bounds(latitude, longitude, radius),
:having => "#{distance} <= #{radius}",
:order => options[:order],
:limit => limit_clause(options)
:limit => options[:limit],
:offset => options[:offset]
}
end

Expand All @@ -106,7 +107,8 @@ def approx_near_scope_options(latitude, longitude, radius, options)
["#{lat_attr} BETWEEN ? AND ? AND #{lon_attr} BETWEEN ? AND ?"] +
coordinate_bounds(latitude, longitude, radius),
:order => options[:order],
:limit => limit_clause(options)
:limit => options[:limit],
:offset => options[:offset]
}
end

Expand All @@ -125,16 +127,6 @@ def coordinate_bounds(latitude, longitude, radius)
longitude + (radius / factor)
]
end

##
# Build the limit clause for a query based on the same options hash
# passed to the x_near_scope_options methods.
#
def limit_clause(options)
if options[:limit] or options[:offset]
"#{options[:offset].to_i},#{options[:limit].to_i}"
end
end
end

##
Expand Down

0 comments on commit dfb6d81

Please sign in to comment.