我安装了Geocoder gem,我想禁用API。我在zipcode表中有47,000个纬度/经度坐标。我想使用我的数据库中的数据,而不是谷歌的API。
我该怎么做才能调用我的数据库中的zipcode表并找到最近的纬度/长距离。
用户模型:
class User < ActiveRecord::Base
has_secure_password
attr_accessible :role, :age, :age_end, :password_confirmation, :about_me, :feet, :inches, :password, :birthday, :career, :children, :education, :email, :ethnicity, :gender, :height, :name, :password_digest, :politics, :religion, :sexuality, :user_drink, :user_smoke, :username, :zip_code
has_many :users, dependent: :destroy
validates_format_of :zip_code,
with: /\A\d{5}-\d{4}|\A\d{5}\z/,
message: "should be 12345 or 12345-1234"
validates_uniqueness_of :email
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
validates_presence_of :password, :on => :create
before_create { generate_token(:auth_token) }
ROLES = %w[admin user guest banned]
# models/user.rb
after_create :setup_gallery
def over_18
if birthday + 18.years > Date.today
errors.add(:birthday, "can't be under 18")
end
end
def age
now = Time.now.utc.to_date
now.year - birthday.year - ((now.month > birthday.month || (now.month == birthday.month && now.day >= birthday.day)) ? 0 : 1)
end
def to_s; username
end
def has_role?(role_name)
role.present? && role.to_sym == role_name.to_sym
end
def send_password_reset
generate_token(:password_reset_token)
self.password_reset_sent_at = Time.zone.now
save!
UserMailer.password_reset(self).deliver
end
def generate_token(column)
begin
self[column] = SecureRandom.urlsafe_base64
end while User.exists?(column => self[column])
end
end
答案 0 :(得分:1)
编写您自己使用的“地理编码器”类。首先查看数据库中的坐标,然后再回到API。像这样的东西(但这是为了使用多个后端):
答案 1 :(得分:-1)
在User.rb
中,执行此操作
def geocode
[self.zip_code.latitude, self.zip_code.longitude]
end
现在运行User.near(my_user.geocode)
如果有帮助,请告诉我。