1

これは簡単なことです。しかし、私は数時間前から狩りをしてきましたが、これを働かせるようには見えません。私は複数のアドレスを持つユーザーがいます。ジオコーダーの宝石を使用してこれらのユーザーを郵便番号検索で表示しようとしています。私のコードはジオコーダーのレールキャストにあるものと非常によく似ています。RailsでのModel Associationの問題

ここに私のコントローラの試み1だと「未定義のメソッド `アドレス」

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を::アドレス』のプロフィールならば(私は知りません

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? 

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 

は、ここに私のモデルです)... .whereビットは動作しますが、そのも、その部分にはなっていません

ありがとうございました!

答えて

1

追加、あなたはすべてのプロファイルを取得するには、以下に変更されることがあります。

+0

これはほぼ完全に機能しました。 .blankを切り替える必要がありましたか? .nilに? – Robert

+0

ページ設定できない配列を返す – Will

0

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

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

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

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

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') 

誰もが(好ましくはスコープで)これを行うためのよりよい、スケーラブルな方法を持っている場合、私はそれを聞いてみたいです。