0

我安装了 Geocoder gem,我想禁用 API。我在邮政编码表中有 47,000 个纬度/经度坐标。我想使用数据库中的数据,而不是 Google 的 API。

我将如何做到这一点,以便它调用我数据库中的邮政编码表并找到最近的纬度/长距离。

用户型号:

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
4

1 回答 1

-1

在你的User.rb,这样做

def geocode
  [self.zip_code.latitude, self.zip_code.longitude]
end

现在运行User.near(my_user.geocode)

让我知道这是否有帮助。

于 2013-11-14T01:59:03.507 回答