1

うまくいけば、これは簡単になります。しかし、私は今数時間狩りをしていて、これを機能させることができないようです。複数の住所を持つユーザーがいます。Geocodergemを使用して、これらのユーザーを郵便番号検索で表示しようとしています。私のコードは、GeocoderRailscastにあるものと非常によく似ています。

これが私のコントローラーの試み1で、「未定義のメソッド`Addresses'」を返します。

def index
  if params[:search].present?
    @profiles = Profile.Addresses.near(params[:search], 25, :order => :distance)
    @title = "Therapists Near " + :search
  else
  @profiles = Profile.all
  @title = "Everyone"
  end
end

これは試行番号2であり、「初期化されていない定数ProfilesController :: Addresses」を返します(Profile.whereビットが機能するかどうかはわかりませんが、その部分に到達することすらできません...)

class ProfilesController < ApplicationController

  def index
    if params[:search].present?
      addresses = Addresses.near(params[:search], 25, :order => :distance)
      @profiles = Profile.where(:id => addresses.id)
      @title = "Therapists Near " + :search
    else
    @profiles = Profile.all
    @title = "Everyone"
    end
  end

これが私のモデルです:

class Profile < ActiveRecord::Base
  has_many :addresses, :dependent => :destroy
  accepts_nested_attributes_for :addresses, :reject_if => lambda { |a| a[:street].blank? }, :allow_destroy => true

class Address < ActiveRecord::Base
  belongs_to :profile
  geocoded_by :street
  after_validation :geocode, :if => :street_changed?

ご覧いただきありがとうございます!

4

3 に答える 3

1

さらに、すべてのプロファイルを取得するには、次のように変更できます。

addresses = Address.near(params[:search], 25, :order => :distance)

@profiles = addresses.map{ |ad| ad.profile }.uniq unless addresses.nil?

一致するアドレスがある場合は、各アドレスからプロファイルを見つけます。

于 2012-01-15T07:46:45.580 に答える
0

Addresses.near(params[:search], 25, :order => :distance)2番目の場合は、からに変更する必要がありますAddress.near(params[:search], 25, :order => :distance)

于 2012-01-15T07:01:23.540 に答える
0

私は受け入れられた答えでページをめくることができなかったことがわかりました。だから私はかなり汚くなった:

addresses = Address.near(params[:search], 25, :order => :distance)
locations_id_array = []  
addresses.each do |address| 
  addresses_id_array << address.id
end  
@profiles  = Profile.where(:address_id => addresses_id_array).paginate(:page => params[:page],  :per_page => 5).order('name DESC')

誰かがこれを行うためのより良い、スケーラブルな方法を持っているなら(できればスコープを使って)、私はそれを聞いてみたいです。

于 2013-01-02T05:09:13.007 に答える