Rails上のジオコーダとの関連付けを検索するには、ActiveRecord :: Realion :: ActiveRecord_relationエラー

StackOverflow https://stackoverflow.com//questions/24005691

質問

私は、登録時にGeocoder GEMを正常にジオコーダしているアドレスがジオコーディングされているポスター(ユーザータイプ)に属する投稿リソースを持っています。今、私はユーザーに距離によってそれらの近くの投稿を検索することができます。私はレールに比較的新しい、あらゆる助けが大いに感謝されるでしょう。

次のエラーを得ています(私はそれが何を意味するのかわかりません、私のモデルの協会を十分に設定したと思いました)

Cannot visit ActiveRecord::Relation::ActiveRecord_Relation_Poster
.

index.html.erb

<%= form_tag posts_path, method: :get do %> 
Find postings <%= select_tag "within", options_for_select([["Next door", 0.001],["In your  neighborhood", 0.05], ["Nearby", 0.1]], ["Nearby", 0.1]) %>
<%= submit_tag "Find", :name => nil %>
<% end %>
.

POSTS_CONTROLLER.ERB

  def index
if params[:within]
  @posts= Post.where(Poster.near(current_user, params[:within].to_f))
else
 @posts=Post.all
 end
end
.

モデル

を投稿します
class Post < ActiveRecord::Base
belongs_to :poster
end
.

ポスターモデル

class Poster < User
has_many :posts, dependent: :destroy   
end
.

ユーザモデル

class User < ActiveRecord::Base
 def full_address
[address_one, city, state, zip].compact.join(', ')
 end 

geocoded_by :full_address, :latitude => :lat, :longitude => :long
after_validation :geocode, if: -> (full_address){ full_address.present? or     address_changed? }
end
.

役に立ちましたか?

解決

投稿の作成に関する属性を渡したばかり、私の元の問題を解決する方法があることを望みます(データベースを冗長にする)

Post Controller

def create
 @post = current_user.posts.build(post_params)
 @post.assign_attributes(:lat => current_user.lat, :long => current_user.long)
end

def index
if params[:within]
 @posts= Post.near(current_user, params[:within].to_f)
else
 @posts=Post.all
 end
end
.

私の投稿モデルで「ジオコーディング:ポスター」を使用しました

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top